Hi guys. I am a beginner at unity. Recently, I wanted to try making something similar to mario and I ran into a problem on the moment of death. I want the game to know that I lost a life and then start the game from the beginning of the level. However it wasn’t working out for me.
I first tried to just reload the level the moment I die and keep a score of how many times I died but this didn’t work out because all the assets in the scene also got reset. I then tried to just re position the player object to the initial game position but then I realized that this wouldn’t work because any previous enemy objects that were there in the scene would be lost on the second round of the game.
Below is the code I made for each instance.
// moving it back to the startPosition
public class Ball_Movement : MonoBehaviour {
public float lives;
void Awake()
{
lives = 3;
}
void OnCollisionEnter2D(Collision2D otherObj)
{
if (otherObj.gameObject.tag == "Trap")
{
Debug.Log("Collided and died");
lives--;
transform.position = startPosition.transform.position;
if(lives<0)
{
Application.LoadLevel(0);
}
Debug.Log("Number of lives left = " + lives);
}
}
}
//Reloading the level
public class Ball_Movement : MonoBehaviour {
public float lives;
void Awake()
{
lives = 3;
}
void OnCollisionEnter2D(Collision2D otherObj)
{
if (otherObj.gameObject.tag == "Trap")
{
Debug.Log("Collided and died");
lives--;
Application.LoadLevel(Application.LoadedLevel);
if(lives<0)
{
Application.LoadLevel(0);
}
Debug.Log("Number of lives left = " + lives);
}
}
}
If anyone has any opinion on what I can do or if anyone can tell me what I am doing wrong, it is greatly appreciated.
Please use code tags when posting code on the forum to make your code readable (edit your post please).
You can reload the level when your character dies like you tried to do in your first attempt. However, if you want to keep track of how many deaths there are, create a controller object that has a deaths variable in a controller script. When you die, increment that variable’s value, and reload the scene.
In order to keep this controller object from disappearing when you reload the level, you will need to use DontDestroyOnLoad():
using UnityEngine;
using System.Collections;
public class ControllerScript : MonoBehavior
{
void Awake()
{
DontDestroyOnLoad(gameObject);
}
...
}
Make sure you use a script to instantiate your controller instead of putting it in the scene. If it’s part of the scene, it will duplicate itself every time you reload. Have a different script check to see if your controller object already exists (try using GameObject.Find()); if it doesn’t, instantiate the controller. This will ensure that there is always exactly one controller object.
You can use this object for tracking all kinds of things between scenes; this structure is referred to generally as the singleton pattern.
Using the above structure isn’t strictly necessary, since you can just create a static variable somewhere called deaths instead, and that variable will always exist in the same place in memory and will hold its value until your project is terminated:
using UnityEngine;
using System.Collections;
public class GameControl
{
static int deaths;
...
}
There are pros and cons to each, some prefer one over the other. Feel free to try both.
Hybladamin, I ended up using the static variable concept because it was easier to implement and it worked quite well. Thanx for ur reply. It helped me a lot. The other method you were explaining seemed interesting too. If possible, can u plz send some more links that explain about the singleton pattern to a beginner and how it is implemented in coding. I am still not an expert at coding and this seemed like an interesting topic.
This topic discusses the same idea that I described. There is also at least one on Unity answers that is very similar. They use a static variable to store a reference to the controller object, and only instantiate a new one if one doesn’t already exist. This way you only need one static variable to track a bunch of game information, rather than a bunch of static variables.
This can be useful for a few reasons:
-Prevents “pollution” of the global namespace. Whether this is important is sometimes debated.
-Also allows a bunch of methods to be run during Update() that need to watch for states or values in every scene, and again hold values between scenes. For example, you can have a section of code in your controller object that counts the number of enemies on the screen, and plays an alarm sound, triggers camera effects, changes GUI colors, etc. to alert the player when there are too many.