-1
\$\begingroup\$
 public GameObject OnComplete;
void Start()
{
    FindObjectOfType<ThirdBossSpawn>().CompletionLevelOne += OnCompletionScreen;
}



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

public void AccomplishScreen()
{
    SceneManager.LoadScene(0);
}

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

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

2 Answers 2

0
\$\begingroup\$

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;
      }
}
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .