Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by mishelbi · Apr 24, 2019 at 09:54 PM · scripting problemdontdestroyonload

Getting MissingReferenceException on Canvas text even though I used DontDestroyOnLoad

I have a questManager script that uses Singleton Instance. When the quest is complete and the next scene is loaded, I'm getting a 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. On the Hierarchy of level02, under DontDestroyOnLoad it shows the canvas and the text so the text shouldn't be destroyed. What can I check to correct this exception? Here is my manager script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class Quest
 {
     //Quest completed status
     public enum QUESTSTATUS { UNASSIGNED = 0, ASSIGNED = 1, GATHERED = 2,COMPLETE = 3 };
     public QUESTSTATUS Status = QUESTSTATUS.UNASSIGNED;
     public string QuestName = string.Empty;
 }
 public class QuestManager : MonoBehaviour
 {
     //All quests in game
     public Quest[] Quests;
     private static QuestManager SingletonInstance = null;
     public static QuestManager ThisInstance
     {
         get
         {
             if (SingletonInstance == null)
             {
                 GameObject QuestObject = new GameObject("Default");
                 SingletonInstance = QuestObject.AddComponent<QuestManager>();
             }
             return SingletonInstance;
         }
     }
     //--------------------------------
     void Awake()
     {
         //If there is an existing instance, then destory
         if (SingletonInstance)
         {
             DestroyImmediate(gameObject);
             return;
         }
 
         //This is only instance
         SingletonInstance = this;
         Transform parent = GetComponentInParent<Transform>();
         Transform text = parent.GetComponentInChildren<Transform>();
         DontDestroyOnLoad(gameObject);
         DontDestroyOnLoad(text);
     }
     //--------------------------------
     public static Quest.QUESTSTATUS GetQuestStatus(string QuestName)
     {
         foreach (Quest Q in ThisInstance.Quests)
         {
             if (Q.QuestName.Equals(QuestName))
                 return Q.Status;
         }
 
         return Quest.QUESTSTATUS.UNASSIGNED;
     }
     //--------------------------------
     public static void SetQuestStatus(string QuestName, Quest.QUESTSTATUS NewStatus)
     {
         foreach (Quest Q in ThisInstance.Quests)
         {
             if (Q.QuestName.Equals(QuestName))
             {
                 Q.Status = NewStatus;
                 return;
             }
         }
     }
     //--------------------------------
     //Resets quests back to unassigned state
     public static void Reset()
     {
         if (ThisInstance == null) return;
 
         foreach (Quest Q in ThisInstance.Quests)
             Q.Status = Quest.QUESTSTATUS.UNASSIGNED;
 
     }
 }
 

 
Comment
Add comment · Show 1
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 xxmariofer · Apr 25, 2019 at 07:42 AM 0
Share

where is exactly the error ocurring? can you print the text gameobject name to make sure is not trying to access another text?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mishelbi · Apr 25, 2019 at 04:35 PM

@xxmariofer It appears to be happening in the QuestGiver script. Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class QuestGiver : MonoBehaviour
 {
     public string QuestName = string.Empty;
     //Reference to UI Text Box
     public Text Captions= null;
     //List of strings to say
     public string[] CaptionText;
 
     //--------------------------------
 
     private void Start()
     {
         Debug.Log(Captions.name);
     }
     void OnTriggerEnter(Collider other)
     {
         if (!other.CompareTag("Player")) return;
 
         Quest.QUESTSTATUS Status = QuestManager.GetQuestStatus(QuestName);
         Captions.text = CaptionText[(int)Status]; //Update GUI text
         Debug.Log(Status);
         if(Status == Quest.QUESTSTATUS.GATHERED)
         {
             QuestManager.SetQuestStatus(QuestName, Quest.QUESTSTATUS.COMPLETE);
         }
        
 
     }
     //--------------------------------
     void OnTriggerExit(Collider other)
     {
         Quest.QUESTSTATUS Status = QuestManager.GetQuestStatus(QuestName);
         if (Status == Quest.QUESTSTATUS.UNASSIGNED)
         {
             QuestManager.SetQuestStatus(QuestName, Quest.QUESTSTATUS.ASSIGNED);
             Quest.QUESTSTATUS StatusIs = QuestManager.GetQuestStatus(QuestName);
         }
         Captions.text = " ";
         if (Status == Quest.QUESTSTATUS.COMPLETE)
         {
             SceneManager.LoadScene(1);
         }
         Debug.Log(Captions.name);
 }
 }
 
 After the quest is complete, after the loading of the new scene. With the debug statement I have there it says QuestText. How do I find what text the exception is referring to. I'm not destroying any text object.
Comment
Add comment · Show 3 · 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 xxmariofer · Apr 26, 2019 at 08:34 AM 0
Share

do you have multiple instance of that script? you can add a breakpoint before the error ocurrs and check what reference is being shown in the inspector

avatar image mishelbi xxmariofer · Apr 26, 2019 at 03:02 PM 0
Share

It's definitely trying to access the QuestText which is included in the dontdestroyonload. It pops up after the new scene loads and when you trigger the NPC no text appears and another exception fires. Is there a way to reinstantiate the text object after it's been destroyed? I'm grasping at straws here. I need to get this fixed. I'm on a deadline which is Sunday and this exception breaks my game at scene load.

avatar image xxmariofer mishelbi · Apr 27, 2019 at 04:41 PM 0
Share

Thats really weird, is always thr same text? You have to beem destroying it or nor setting uo the dont destroyonload, i cant tell you more since is q debug question and i would need to see whatsa happening, unity has a OnDestroy event, afd it to all the text and check which and when is getting destroyed, and yoy can reinstantiate another copy inside the ondestroy if you dont find the bug

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

180 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 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 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 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 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 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 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 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

How do I transfer a gameobject created in one scene to another scene AND use it in a c# script? 1 Answer

Execution order of Destroy() and DontDestroyOnLoad() between Scenes 0 Answers

Why does my Player Object's Transform get destroyed when loading a new scene? 0 Answers

How to stop objects you picked up from reappearing when you go to another level and return 1 Answer

How to disable DontDestroyOnLoad on throwables? 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