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

**URL:** https://discussions.unity.com/t/solved-how-do-i-execute-something-when-nullreferenceexception-activates/647036
**Category:** Scripting
**Tags:** Scripting
**Created:** [November 26, 2016, 6:13am UTC](https://discussions.unity.com/t/solved-how-do-i-execute-something-when-nullreferenceexception-activates/647036 "2016-11-26T06:13:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Gunging](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gunging/32/187923_2.png) [@Gunging](https://discussions.unity.com/u/Gunging)
#### Post date: [November 26, 2016, 6:13am UTC](https://discussions.unity.com/t/solved-how-do-i-execute-something-when-nullreferenceexception-activates/647036/1 "2016-11-26T06:13:41Z")

</div>

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:

```csharp
        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.

---

<div class="post-metadata">

### Author: ![GroZZleR](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/grozzler/32/316385_2.png) [@GroZZleR](https://discussions.unity.com/u/GroZZleR)
#### Post date: [November 26, 2016, 6:39am UTC](https://discussions.unity.com/t/solved-how-do-i-execute-something-when-nullreferenceexception-activates/647036/2 "2016-11-26T06:39:02Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![Gunging](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gunging/32/187923_2.png) [@Gunging](https://discussions.unity.com/u/Gunging)
#### Post date: [November 26, 2016, 5:51pm UTC](https://discussions.unity.com/t/solved-how-do-i-execute-something-when-nullreferenceexception-activates/647036/3 "2016-11-26T17:51:44Z")

</div>

Yay it worked thanx so much
