- Home /
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;
}
}
where is exactly the error ocurring? can you print the text gameobject name to make sure is not trying to access another text?
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.
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
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.
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

Follow this Question
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