Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
3
Question by pdrinovac2 · Jul 27, 2015 at 10:41 AM · uitextscenesroll a ball

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!\n" +
                 "Touch the yellow cubes for points!\n" +
                 "Collect them all to win!\n" +
                 "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!\n";
             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!\n\n\n\n" +
             "You won with a score of " + score.ToString ();
         if (thisLevel != finalLevel) {
             winText.text += "\nYou may press the Space key"
                 + "\nto jump to the next level.";
         } else {
             winText.text += "\nYou 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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by ScienceIsAwesome · Jul 28, 2015 at 05:07 AM

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pdrinovac2 · Jul 28, 2015 at 07:02 AM 0
Share

Hmm, does that mean I should make my canvas a child of my player? I don't have a script for the UI; just the player, the camera, and one for rotating an object.

avatar image ScienceIsAwesome · Jul 28, 2015 at 11:01 AM 0
Share

Or drop a DontDestroy$$anonymous$$e script on any gameobject you want to keep alive

avatar image pdrinovac2 · Jul 28, 2015 at 04:41 PM 0
Share

Would that honestly just be as simple as just writing a script that only says public static T instance == null; void Awake() { if (instance == null) instance = this; else Destroy (transform.gameObject); DontDestroyOnLoad (transform.gameObject); }

I don't think using T would work but if I wanted to use it multiple times, I wouldn't want to have to write a script for every different type of object in my game.

avatar image pdrinovac2 · Jul 28, 2015 at 08:12 PM 0
Share

I got it to work!! Thanks a lot man!!

avatar image meat5000 ♦ · Jul 28, 2015 at 08:14 PM 0
Share

Click the tick to accept the answer

Show more comments
avatar image
18

Answer by Michio-Magic · Oct 25, 2015 at 12:28 AM

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 :)

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image firestoke · Oct 29, 2015 at 06:51 AM 0
Share

Thank you a lot!! I meet the same situation like you. Just follow what u said: "unlock" the checkbox, then problem is solved... ~"~

avatar image IanSmellis · Nov 09, 2015 at 04:09 PM 0
Share

Thank You! Sometimes it is always the small things. face palm

avatar image jammingames · Nov 11, 2015 at 03:19 AM 0
Share

can confirm this was exactly the problem for me. I was worried searching for this bug would lead to mostly beginner issues about null references. I had a locked inspector window on second monitor behind other things. Unity should bring all inspector windows to the foreground ideally when play /stop play are pressed in editor. This would've had me scratching my head for an hour if I didn't read this answer!

avatar image Will-D-Ramsey · Jan 11, 2016 at 09:12 PM 0
Share

Thanks man, no change in code needed, Upvoted!

avatar image Ardinius · Feb 29, 2016 at 08:20 PM 0
Share

thank you for this tip $$anonymous$$ichio $$anonymous$$agic

Show more comments
avatar image
18

Answer by kenmgrimm · Sep 25, 2017 at 07:01 PM

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jbusuito-milo · Dec 22, 2017 at 08:24 PM 1
Share

Thanks for this answer! In my case, the error only occurred when loading a different scene and returning to the original later, and some lingering event handlers were indeed the issue.

avatar image RodrigoSeVeN · Feb 17, 2018 at 05:03 PM 2
Share

I had scene objects subscribed to a delegate in a static class. After unsubscribing them using OnDestroy() it went back to normal. Thank you for the help!

avatar image OdColmen · Aug 11, 2018 at 03:53 PM 0
Share

Thanks for the wisdom! Unsubscribing to some events on OnDestroy solved it in my case.

avatar image Geri860 · Aug 27, 2021 at 10:23 AM 0
Share

You've deserved my rep point. Holy sh, I was trying to resolve something similar for at least 3 hours, and you've solved it instantly.

avatar image ghastlysmilegames · Sep 29, 2021 at 03:11 PM 0
Share

Pretty old answer but very useful so I wanted to give it a +1, thank you very much.

As an addendum, here is the code to use in your scripts attached to GameObjects that you are destroying:

 // Let's say you have this in the Start method
 private void Start()
 {
     EventManager.onEvent += OnEvent;
 }
 
 // And a method that fires when receiving the Event
 private void OnEvent()
 {
     StartCoroutine(MyCoroutine());
 }
 
 private IEnumerator MyCoroutine()
 {
     while(true)
         yield return null;
 }
 
 private void OnDestroy()
 {
     // Replace EventManager.onEvent with whatever class is sending event messages to subscribers
 
     // Unsubscribe from event(s)
     EventManager.onEvent -= OnEvent;
 
     // And stop all coroutines
     StopAllCoroutines();
 }


avatar image
1

Answer by FlightOfOne · Jan 26, 2018 at 06:17 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Necrohunter · Jun 16, 2020 at 01:21 AM

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();

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Display timer to Roll a Ball Tutorial 1 Answer

How to make conversation UI similar to Pillars of Eternity or Planescape:Torment 0 Answers

Draw calls and text on UGUI 0 Answers

How can I check if there is no free space in the Text component? 1 Answer

One textUI and two colliders that update it 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges