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;
}
}