[Solved] How do I execute something when NullReferenceException activates?

Well, I was doing this time survival thing, lasts for 60 seconds, the player has a public “died” bool, and the background image secretly ends the game, and detects if the player has died, I have coded:

        if (Time.time >= endDelay) {
            if (GameObject.FindWithTag("Player").GetComponent<Movement>().died == true) { Application.LoadLevel("Looose"); }
            if (GameObject.FindWithTag("Player").GetComponent<Movement>().died == false) { Application.LoadLevel("Win"); }
        }

*Movement is the script I coded all the player stuff (originaly was only to code movement keys but I ended up putting everything)
*endDelay is a float calculated when the Time Survival starts with: endDelay = Time.Time+60

But well, when the player dies the GameObject is destroyed, so it cant detect a player with bool died in true cause there will be no player to detect xD, so I want Application.LoadLevel(“Looose”) to run when no player is detected (Causing a NullReferenceException).

I think I read somewhere there was a simple line to do this. Thanx for any help.

GameObject player = GameObject.FindWithTag("Player");

if(player == null)
{
 // no player, do whatever
}
1 Like

Yay it worked thanx so much