NullReferenceException MonsterSpawner

Hey guys, I’m trying to write a script that spawns monsters randomly in Unity and apparently there’s something wrong with my code in line 43 that I can’t seem to figure out. Hope that you can help me:)))

This is the error:

NullReferenceException: Object reference not set to an instance of an object
MonsterSpawner+d__7.MoveNext () (at Assets/Scripts/MonsterSpawner.cs:43)

… and this is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonsterSpawner : MonoBehaviour
{

    [SerializeField]
    private GameObject[] monsterReference;

    private GameObject spawnedMonster;

    [SerializeField]
    private Transform leftPos, rightPos;

    private int randomIndex;
    private int randomSide;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(SpawnMonsters());
    }

    IEnumerator SpawnMonsters() {

        while (true) {

            yield return new WaitForSeconds(Random.Range(1, 5));

            randomIndex = Random.Range(0, monsterReference.Length);
            randomSide = Random.Range(0, 2);

            spawnedMonster = Instantiate(monsterReference[randomIndex]);

            // left side
            if (randomSide == 0)
            {

                spawnedMonster.transform.position = leftPos.position;
                spawnedMonster.GetComponent<Monster>().speed = Random.Range(4, 10);

            }
            else
            {
                // right side
                spawnedMonster.transform.position = rightPos.position;
                spawnedMonster.GetComponent<Monster>().speed = -Random.Range(4, 10);
                spawnedMonster.transform.localScale = new Vector3(-1f, 1f, 1f);

            }

        } // while loop

    }


} // class

Remember, the error it self is the most useless part of the message, Look for the informative parts

  • Script, Line and Column it is in
  • Key words in the error

Remember Object not set to an instance of an object is one of the most common errors. Ways to fix it are

  • Something isn’t unassigned in the inspector
  • Missing reference exception

Try solve it your self, check the line the error is on. Ask your self questions
Is their a script called Monster, have you assigned spawnedMonster in the inspector?

So you know line 43 is where the error is happening. Look at the line above with code, you are trying to use GetComponent and access the speed variable of the Monster script. Start your search there.

Incase anyone in the future is trying to solve this, the fix here is not your code it is that you accidently added the monsterspawner script to your monster prefabs. If you remove monsterspawner script from your prefabs the errors go away and everything works right. You should only have the monster script attached to the prefabs.

And it does not even matter, monsters or flowers or cookies or turnips: it is ALWAYS the same three-step process to fix a null reference:

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

If you are NOT doing that three-step process, you are simply wasting time until you buckle down and do it.