Another Level Loads when Object is destroyed

Hi guys, I posted a Question but it has been some hours now, (10) and a mod has not seen it. I was told by somebody to post it on here too. here is my question. Copy and Paste!

I am trying to work on a script so when the ‘Player’ is destroyed a level will load. The Script is working but it loads the level instantly. It does not wait for the play to die.

using UnityEngine; using System.Collections;

public class GameOver : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if(GameObject.FindGameObjectsWithTag(“Player”).Length <= 1) Application.LoadLevel(“0”);

}

}

Also I should say that the only way that the player can be destroyed is by the collider with the enemy block. I don’t know why I wrote that script as it was but hey its 3:30AM! =D

Also how would one write a script to make it so when all the Objects with a certain tag has been Destroyed it loads a new level?

Thanks guys!

The documentation of GameObject.FindGameObjectsWithTag says “Returns a list of active GameObjects tagged tag. Returns null if no GameObject was found.”, so I would check for a null pointer before accessing the “Length” value. Actually, just checking that it returns null to load the level would be enough no?

As for you current code, you’re checking with “less or equal to 1” so it’s normal that even when the player is still alive, the condition is true.

OK so let me make sure I got this.

I have made sure that ‘Player’ is tagged Player.

So making the number higher would be best?

Would it make sense to stick in an if statement to say if the GameObject is destroyed then it will LoadLevel();

I am new to C# I work with HTML and CSS trying to build my way into C#.

What I meant was something like this:

GameObject[] playerGameObjects;
playerGameObjects = GameObject.FindGameObjectsWithTag("Player");
if( playerGameObjects == null )
{
    Application.LoadLevel("0");
}

Or since you don’t need to get an array of objects but just the player, you can try GameObject.FindWithTag

GameObject playerGameObject;
playerGameObject = GameObject.FindWithTag("Player");
if( playerGameObject == null )
{
    Application.LoadLevel("0");
}

Why not simplify it further?

void OnDestroy() {

//call when player gets destroyed

Application.loadlevel("0");

}

Thanks guys! That worked great.

Could these scripts be used to a similar type To make it so when so many Tagged GameObjects get Destroyed it that

Application.LoadLevel?