Unity - Error: Object reference not set to an instance of an object - Instantiate

1

I've done the Survival Shooter tutorial that can be found in the Unity asset store at link

The game is working, but is giving this following error in the code. How do I resolve this?

NullReferenceException: Object reference not set to an instance of an object EnemyManager.Spawn () (at Assets/Scripts/Managers/EnemyManager.cs:26)
(Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);)

This error appears just when the game starts and the dolls begin to attack the player.

The code is the same as the tutorial for your channel and the unity website:

using UnityEngine;

public class EnemyManager : MonoBehaviour
{
    public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    public GameObject enemy;                // The enemy prefab to be spawned.
    public float spawnTime = 3f;            // How long between each spawn.
    public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.


    void Start ()
    {
        // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
        InvokeRepeating ("Spawn", spawnTime, spawnTime);
    }


    void Spawn ()
    {
        // If the player has no health left...
        if(playerHealth.currentHealth <= 0f)
        {
            // ... exit the function.
            return;
        }

        // Find a random index between zero and one less than the number of spawn points.
        int spawnPointIndex = Random.Range (0, spawnPoints.Length);

        // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
        Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    }
}
    
asked by anonymous 22.03.2015 / 03:26

1 answer

4

Understanding the Problem

The error Object reference not set to an instance of an object happens when you work with some variable that was not instantiated.

In Unity, the Instantiate method can be a two-way user.

  • Instantiate (original: Object);
  • Instantiate (original: Object, position: Vector3, rotation: Quaternion);
  •   

    link

    In your code you pass enemy and spawnPoints

    Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    

    And are created and instantiated in:

    public GameObject enemy;                // The enemy prefab to be spawned.
    public Transform[] spawnPoints;
    

    You pass it as a parameter by Inspector of your game object EnemyManager .

    There should be something like this:

    Ifsomethingismissing,thisproblemcanhappen

    PossibleSolutions

    Irecommendyoutoseetwothings:

  • IfallspawnPointsexistinthehierarchyofyourunityasshownbelow.

  • If your images hit mine it might work. If not, it is important that you share more information.

    Add a screenshot of your elements to help investigate if that answer does not help.

        
    22.03.2015 / 05:18