Hey there, Gamers and game Makers! In this week's blog, we're going to build on what we did last week where we made it so we could change scenes when we got to the end of our level. We're going to build on that by making a similar script that will reload the scene should you die by hitting a spike platform. The main difference with this is that we aren't loading a scene by name exactly but rather reloading the currently active scene. So in other words, we don't need to make a bunch of these scripts or keep having to update scene names for it to work. It will simply reload the current scene. Let's get started. As you can see, I've added a spike platfrom to my level and on that I've added a 2D box collider and set it to Is trigger. I've also once again created a new Tag in the project settings called reload and set the spikes tag to reload. Next, create a new C# script called ReloadLevel and open it in Mono Develop. This script is going to be very similar to our load level script from before. Start off by adding using UnityEngine.Scenemanagement to make use of that library in this script. Create a private void OntriggerEnter2D and set it's parameters to be a Collider2D and the tag reload. Now once again we'll be writing an if statement to check if the player collides with the object with the tag reload. If such a collision is detected we won't reload the scene by name like we did before for our change level script. We want to be able to use this script in any scene without having to edit it every time. So we'll have it reload the active scene by writing Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name); Save the script and now add it as a component to the spikes object. If you run the scene now and let the player fall onto the spikes, you'll see the scene get's reloaded. Try using this script on an object in another scene and you should see that it still works.
Until next time! |
Archives
April 2019
Categories |