# How do I check for nulls when called an event from another script?

 public GameObject OnComplete;
void Start()
{
FindObjectOfType<ThirdBossSpawn>().CompletionLevelOne += OnCompletionScreen;
}

void OnCompletionScreen()
{
OnComplete.SetActive(true);
}

public void AccomplishScreen()
{
}


I got nullreference error on this error " FindObjectOfType<ThirdBossSpawn>().CompletionLevelOne += OnCompletionScreen;" But I'm not sure how to check for nulls in terms on this situation considering "ThirdBossSpawn" is from another script

• assigning the result of FindObjectOfType<ThirdBossSpawn>() to a variable and checking this against null before setting the listener is not working for you? May 9 at 17:05
• Are you sure the script is attached to a GameObject in the hierarchy? May 10 at 16:20

Create a class where you store all the boss references. Let's call it BossSpawnManager Then cache it in your script and use it

public BossSpawnManager bossSpawnManagerRef;

void Start()
{
if (bossSpawnManagerRef != null)
{
bossSpawnManagerRef.CompletionLevelOne += OnCompletionScreen;
}
}


The same way you check for nulls for literally anything else.

void Start()
{
var thirdBoss = FindObjectOfType<ThirdBossSpawn>()

if (thirdBoss != null) thirdBoss.CompletionLevelOne += OnCompletionScreen;
}


But honestly, if you're getting a null here, it means you have set up your scene incorrectly. You have a script that depends on a ThirdBossSpawn script when no such script instance can be found. So just suppressing the null error won't fix this more fundamental problem.