How to initiate a GameOver with Lives

Hi I am new to Unity and I have a basic game and i was wondering in C# how i could do something for when the players lives reach 0 it initiates a screen saying GameOver thank you!

-Galus07

/
/
/
public int Lives = 3;


if (Player.Lives > 0)
		{
		    Destroy.gameObject();
                                        Application.LoadLevel(3);	
		}
/
/
/

A simple example:

public int lives=3;

void Update () {
    if (lives <= 0) {
        Application.LoadLevel ("GameOver");
    }
}

A better example:

private int lives=3;

// called whenever a life is lost
void LoseLife () {
    lives--;
    if (lives <= 0) {
        Application.LoadLevel ("GameOver");
    }
}

The second is more efficient is it isn’t called every frame, only when needed.

Hi Dman i tried your second one and it didn’t seem to work. when I have my levels do i have to include the .unity in GameOver.unity? also here is my code with your addition Dman yet it still does not work. Any suggestions? Thank you!

public class PlayerStatusController : MonoBehaviour {
	
	private int Lives = 5;
	
	public ParticleEmitter EnemyExplosionEffect;
	
	void LoseLife () {
		Lives--;
		if (Lives <=0) {
			Application.LoadLevel ("GameOver");
		}
	}
	
	public void OnGUI() {
		GUI.Label(new Rect(20, 20, 100, 20), System.String.Format("LIVES: {0}", Lives));
	}
	
	public void OnTriggerEnter(Collider collider) {
		Lives--;
		
		Vector3 enemy_position = collider.transform.position;
		Destroy (collider.gameObject);
		
		ParticleEmitter explosion = (ParticleEmitter)Instantiate(EnemyExplosionEffect, enemy_position, Quaternion.identity);
		explosion.Emit();
		
		gameObject.GetComponent<ForceBlast>().Fire();
	}
}

Create a GameOver scene.

i did

I think you need to put the LoseLife () ; in the Ontrigger function…

Yes, you need to call LoseLife in OnTriggerEnter rather than Lives–

Also, make sure that the GameOver scene is in your build settings.

Sorry I’m completely new (only used for like 2 days) so I’m not catching all of what you mean and what I’m supposed do sorry…

ok so i think i did what you said but now its saying that LoseLife does not exist in the current context here is my code now…

public class PlayerStatusController : MonoBehaviour {
	
	private int Lives = 5;
	
	public ParticleEmitter EnemyExplosionEffect;
	
	public void OnGUI() {
		GUI.Label(new Rect(20, 20, 100, 20), System.String.Format("LIVES: {0}", Lives));
	}
	
	public void OnTriggerEnter(Collider collider) {
		LoseLife (); 
		Lives--;
		if (Lives <=0) {
			Application.LoadLevel ("GameOver.unity");
		}
		
		Vector3 enemy_position = collider.transform.position;
		Destroy (collider.gameObject);
		
		ParticleEmitter explosion = (ParticleEmitter)Instantiate(EnemyExplosionEffect, enemy_position, Quaternion.identity);
		explosion.Emit();
		
		gameObject.GetComponent<ForceBlast>().Fire();
	}
}

You also deleted the LoseLife function…

Add that back in, and delete the Lives-- (along with the life check) you have in OnTriggerEnter and it should work.

You still need this-

void LoseLife () {
8.        Lives--;
9.        if (Lives <=0) {
10.            Application.LoadLevel ("GameOver");
11.        }
12.    }

Just put LoseLife(); int he Ontrigger.

Umm when i add the void before Lose Life it says that void can’t be used in this context

The above code does not go into the OnTriggerEnter function, it goes outside it.

void LoseLife () {
// blabla
}

public void OnTriggerEnter () {
    LoseLife ();
    // blabla
}

Also, in case you didn’t notice the numbers inside his code is a copy+paste error.

alright it work thanks so much!