Forum begins after the advertisement:


[Part 7] NullReferenceException on EnemySpawner.OnDestroy()

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 7] NullReferenceException on EnemySpawner.OnDestroy()

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #12437
    a
    Level 7
    Former Patron
    Helpful?
    Up
    0
    ::

    I have removed the error in the drop manager, but instead another error appears. like this picture

    Screenshot-234

    #12447
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Hi there, this is HAKI right?

    I think the issue is with the OnDestroy() function in your code. You need to add the line in the green highlight below.

    
        private void OnDestroy()
        {
            EnemySpawner es = FindObjectOfType<EnemySpawner>();
            if(es) es.OnEnemyKilled();
        }
    

    If there is no EnemySpawner in the Scene, then es.OnEnemyKilled() will fail because es is null (hence NullReferenceException), so you need to do a check before you run it.

    To learn more about null reference exceptions, you can watch this video. We made it because this error is the most common one that beginners run into.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: