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

Asked

Viewed 13,084 times

2

I made the Survival Shooter tutorial that can be found in the Unity Asset store at https://www.assetstore.unity3d.com/en/#! /content/21028

The game is working, but is giving this following error in the code. How do I solve 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 start attacking the player.

The code is as follows, which 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);
    }
}
  • 1

    Hello @Claudemir, could you please add a screenshot of the Enemymanager Object Inspector - click on it in Hierarchy. It seems that he is not finding the enemies or has not found the spawnpoint.

1 answer

5

Understanding the Problem

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

In Unity the method Instantiate can be used in two ways

  1. Instantiate(original: Object);
  2. Instantiate(original: Object, position: Vector3, rotation: Quaternion);

http://docs.unity3d.com/ScriptReference/Object.Instantiate.html

In your code you pass enemy and the 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 object of play EnemyManager.

There should be something like that:

EnemyManagerInspector

If one is missing it can happen this problem

Possible Solutions

I recommend you see two things:

  1. if the Prefab of all characters exist (Zombunny, zombear and hellephant), they may have disappeared during the migration to Unity 5 that happened in recent weeks.

  2. If all spawnPoints exist in your Unity’s Hierarchy as shown below. Hierarchy

If your images match mine it might work. Otherwise it is important that you share more information.

Add a screenshot of your elements to help with the investigation if that answer doesn’t help you.

  • Hi Nills, I had the same problem with instant but it is with conversation 'Messagingclientreceiver(). start' if you want to help me tell me that I ask the right question and send the link

  • @Leonardov.Degasperin how are you using Instantiate with Messagingclient? You don’t need to invite a user to open your question, create and describe your problem well that other people can help you. Hug.

  • Hi Nils I know you don’t need but we could share answers of the questions was what I thought. However I’m here to help you I’ll give you a link that helped me a lot there has a post of my I’ll pass later, because I have to rediscover, this site is from a game developer of the game desenvelopent team very good and helped me very http://darkgenesis.zenithmoon.com I hope you can help too.

  • http://darkgenesis.zenithmoon.com/DarkGenesisForums/topic/ch5-messagingclicliceiver/ here I got the answers to your question perfectly. my post ta la down I asked for Simon on July 4th

  • 1

    @Leonardov.Degasperin thanks for the tip, the guy gives a legal force to the guys who want to develop.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.