- Home /
Disabling UI destroys it and loses reference.
I have a UI GameObject that I am trying to activate just before I reload the current scene. I use the standard activate script:
public void Scoreboard()
{
scoreBoardUI.SetActive(true);
}
This works fine the first time the scene loads up, but when the scene reloads and I get to the point to where I call the function, I get this error:
MissingReferenceException: The object of type 'GameObject' 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. GameManager.Scoreboard () (at Assets/Scripts/GameManager.cs:108) GameManager.Update () (at Assets/Scripts/GameManager.cs:52)
I know this error is occurring because the UI object is not active, and that Unity cannot find an inactive GameObject.
Can anyone help me restore the reference? I know it has something to do with Awake or Start because I have another UI Element that activates upon Awake and that one works just fine.
Thanks in advance!
Answer by jeffreyrampineda · Nov 23, 2017 at 06:25 PM
Try using DontDestroyOnLoad()
on your UI gameObject https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Thank you! I don't know why that didn't cross my $$anonymous$$d in the first place... especially since I'm using that on my Game$$anonymous$$anager already!
public static GameCanvas canvas; public void Awake() { if (canvas == null) { DontDestroyOnLoad(gameObject); canvas = this; }else if (canvas !=this) { Destroy(gameObject); } }
Thanks for your swift answer to my issue!
Your answer
![](https://koobas.hobune.stream/wayback/20220612143234im_/https://answers.unity.com/themes/thub/images/avi.jpg)