Dan Kenny Game Design
  • Home
  • Portfolio
    • Game Design
    • 2D & Animation
  • Blog
  • About Me
  • Contact
  • Home
  • Portfolio
    • Game Design
    • 2D & Animation
  • Blog
  • About Me
  • Contact

Platformer Tutorial: Reload on Death

29/5/2018

Comments

 
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.

Picture
Picture
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.
Picture
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);
Picture
Save the script and now add it as a component to the spikes object.
Picture
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!

Comments

    Archives

    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    August 2014
    November 2013
    September 2013

    Categories

    All

    RSS Feed

© COPYRIGHT 2020. ALL RIGHTS RESERVED.