MissingReferenceException: The object of type 'Text' has been destroyed but you are still trying to access it.

Okay so I’m making my own changes to the Roll-a-Ball tutorial from the Unity site: https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial

I’m trying to add multiple levels in different scenes and I’m having trouble bringing the text over between the scenes. I keep getting this error when I touch a collectible in the second scene.

MissingReferenceException: The object of type ‘Text’ 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.

My code is here:

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

public class PlayerController : MonoBehaviour {

	public float speed;
	public int score = 0;
	public int thisLevel;
	public Text countText;
	public Text scoreText;
	public Text winText;
	public Text startText;
	public AudioClip mwam;
	public AudioClip sparkle;
	public int[] countMax;
	public static PlayerController instance = null;

	private Rigidbody rb;
	private int count;
	private int finalLevel = 2;
	private bool win;

	// Update () is called before rendering a frame.
	// FixedUpdate () is called just before computing a physics calculation.
	// CTRL + ' searches API for highlighted text.

	void Start () {
		rb = GetComponent<Rigidbody> ();
		count = 0;
		win = false;
		winText.text = "";

		if (thisLevel == 0) {
			startText.text = "Welcome!

" +
"Touch the yellow cubes for points!
" +
"Collect them all to win!
" +
“Black cubes take away points; stay away!”;
} else {
startText.text = “”;
}

		SetCountScoreText ();
	}

	void FixedUpdate () {
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");

		Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);

		rb.AddForce (movement * speed);

		if (win && Input.GetButton("Jump") && (thisLevel != finalLevel)) {
			Application.LoadLevel (thisLevel + 1);
			transform.position = new Vector3 (0, 1, 0);
			count = 0;
		}
	}

	void OnTriggerEnter (Collider other) {
		if (other.gameObject.CompareTag ("PickUp")) {
			other.gameObject.SetActive (false);
			count = count + 1;
			score = score + 50 * count;
			startText.text = "";
		}
		else if (other.gameObject.CompareTag ("BadItem")) {
			score = score / 2;
			AudioSource.PlayClipAtPoint (mwam, Camera.main.transform.position);
		}
		else if (other.gameObject.CompareTag ("GrandPickUp")) {
			other.gameObject.SetActive (false);
			count = countMax[thisLevel];
			score = score + 50 * countMax[thisLevel];
			startText.text = "";
			winText.text = "You found the secret tree!

";
AudioSource.PlayClipAtPoint (sparkle, Camera.main.transform.position, 0.25F);
}
SetCountScoreText ();
}

	void SetCountScoreText () {
		countText.text = "Count: " + count.ToString ()
			+ "/" + countMax[thisLevel];
		scoreText.text = "Score: " + score.ToString ();
		if (count >= countMax[thisLevel] && !win) {
			SetWinText ();
		}
	}

	void SetWinText () {
		win = true;
		winText.text += "Congratulations!

" +
“You won with a score of " + score.ToString ();
if (thisLevel != finalLevel) {
winText.text += "
You may press the Space key”
+ "
to jump to the next level.“;
} else {
winText.text += "
You may now close the game.”;
}
}

	void Awake() {
		if (instance == null)
			instance = this;
		else
			Destroy (transform.gameObject);
		DontDestroyOnLoad (transform.gameObject);
	}
	
}

I don’t know what text I’m destroying but if someone could help, that’d be great. I really just can’t pinpoint where the problem is coming from.

Something similar happened to me:

“MissingReferenceException: The object of type __ 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.”

i had this for an hour trying to get rid of it by disabling every script and game object. i finally realised it was because I had a second inspector window locked (upper right icon) so I could set some variables in the inspector. When I unlocked it, this error went away.

Hope this helps someone else to not lose their mind :slight_smile:

Another common cause of this kind of thing is not cleaning up event listeners or coroutines that access game objects that aren’t set to DontDestroyOnLoad. When you switch scenes and reload the original, if there are any event handlers or coroutines still running that reference the now destroyed (and re-created) game objects you’ll get this error.

Text is a part of the UI, probably in a separate gameobject which is destroyed upon loading. You need to preserve its gameobject with DontDestroyOnLoad() or it will be wiped out upon scene loading. You can also make it a child of another gameobject with the DontDestroyOnLoad() in effect.

i also went into this, but the above answers didnt help me.

My solution was:

i had a static list in some script and i did access it after reloading the scene.
because it was static, it saved the old variables (or something like that?) and told me i cant access them because they are destroyed… but still exists in the static list.

so just do on your start method on the very top:
YourListName.Clear();

I had the same problem. I am sharing this in case someone has the same issue, due to a similar scenario as I mine.

I was getting this Error:

MissingReferenceException: The object
of type ‘TextMeshProUGUI’ 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.

I had nested canvases and one of canvases had a TextMeshPro (text) directly attached to it. Once I removed that, the error went away and the Event system started work again. Guess I can’t directly attach a TMPro to a canvas.