- Home /
How to make player lose lives?
I'm making a 2d platformer, and I want the player to lose lives when he dies. Here's what I've got so far:
public var Lives = 3;
public function OnGUI() {
(GUI.Button (Rect ( 10, 50, 100, 30 ), "Lives: " + Lives));
}
public function OnTriggerEnter (hit : Collider) : IEnumerator {
if (hit.collider.tag == "DeathZone") {
Application.LoadLevel(0);
Lives = Lives - 1;
}
}
Also how would I print the number of lives onto the screen without using a GUI.Button? Sorry I'm pretty new to this. :)
Answer by appearance · Feb 27, 2013 at 06:45 AM
You are decreasing lives @line: 12, but before that "Application.LoadLevel(0)" will load the level again. Hence, your script which holds the Lives var will also get reset and your player never looses lives.
You have to separate the Lives data from this class or do not use Application.LoadLevel().
See how you can manage and come up with new code blocks if you still face the problem.
Answer by robertbu · Feb 27, 2013 at 06:58 AM
You can display the information using GUI.Label. @appearance is right about your data getting reset each time you do an Application.LoadLevel(). If you want to continue to use this method, take a look at Object.DontDestroyOnLoad().
Answer by kkyriakos · Oct 09, 2016 at 07:49 AM
There are lots of cheap plugins out there that will help integrate lives in your game very easily. I recommend "Life System Pro - Easy Lives Integration for Freemium Games" found here https://www.assetstore.unity3d.com/en/#!/content/69842