Missing Reference Exception

So here is the thing. My goal is to make the player destroy when it hit the obstacle. Here’s the code for that

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

public class DestroyPlayer : MonoBehaviour
{
    void OnCollisionEnter(Collision other)
    {
        if(other.gameObject.tag == "Obstacle")
        {
            Destroy(gameObject);
        }
    }
}

But then when I try to play it and try to hit the player into an obstacle, it throws a lot of error saying
“Missing Reference Exception: The object type ‘Transform’ has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.”
Can someone help me? I’m new to Unity and C#

You should copy and paste the error here so we can see all the info on it. Chances are you are still doing some interaction with the player in another script that is throwing the error because you are destroying the player.

You can also double click most errors to have it take you to the line of code that is throwing the error.

If you plan to destroy the player, you need to consider what else you need to do. Do you have other scripts targeting the player? Do you have GUI you need to display? Pause the game? Stop player movement? What other things do you need to happen when a player dies.

I would also suggest, unless this is a single life game, that you consider not destroying the player, but resetting them instead. You’ll maintain your references to the player, you just need to do some “clean up” work like resetting player position, health, resetting enemies or collected items, whatever it is that works for your game.

1 Like
using UnityEngine;

public class FollowPlayer : MonoBehaviour
{

    public Transform player;
    public Vector3 offset;

    // Update is called once per frame
    void Update()
    {
        transform.position = player.position + offset;
    }
}

I add this code as a component to my main camera, score UI, and trail (Some kind of smoke which is made from particle system)

This is the script that throwing an error. I don’t have any GUI to display, I don’t want to pause the game, And yes I want to stop player movement. Yes even tho this is a singlie life game, I still want to destroy the player.

Then you have to stop the camera from calling player.position. Exactly as the error tells you. You can either disable the FollowPlayer script when the player dies or add a bool that you set to false so even when Update runs, it no longer tries to follow your player.

2 Likes

If you are purposely making player null by destroying it, then just don’t try using it when it is null.

public class FollowPlayer : MonoBehaviour
{

    public Transform player;
    public Vector3 offset;

    // Update is called once per frame
    void Update()
    {
        if (player != null)
        {
            transform.position = player.position + offset;
        }
    }
}

Otherwise if you want to keep using it, don’t make it null. Either assign something else to player before destroying it, or don’t destroy it.

1 Like

Thank you, it really does actually work.

using UnityEngine;
using System.Collections;

public class NewBehaviourScript2 : MonoBehaviour
{

    GameObject[] spawnPoint;
    public GameObject zombie;
    public float minSpawnTime = 0.2f;
    public float maxSpawnTime = 1;
    private float lastSpawnTime = 0;
    private float spawnTime = 0;
    // Use this for initialization
    void Start()
    {
        spawnPoint = GameObject.FindGameObjectsWithTag("Respawn");
        UpdateSpawnTime();
    }

    void UpdateSpawnTime()
    {
        lastSpawnTime = Time.time;
        spawnTime = Random.Range(minSpawnTime, maxSpawnTime);
    }

    void Spawn()
    {
        int point = Random.Range(0, spawnPoint.Length);
        Instantiate(zombie, spawnPoint[point].transform.position, Quaternion.identity);
        UpdateSpawnTime();
    }

    // Update is called once per frame
    void Update()
    {
        if (Time.time >= lastSpawnTime + spawnTime)
        {
            Spawn();
        }
    }
}

There is no reason to post in the forum for Missing Reference.

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

https://forum.unity.com/threads/nullreferenceexception-object-reference-not-set-to-an-instance-of-an-object.1108865/#post-7137032