Disabled Canvas is considered Destroyed

Hi,

Have a number of UI canvases, which are set active / inactive as required. This Canvas object is used in multiple scenes. When activating a disabled in certain scenes, the canvas is considered destroyed by Unity, yet it works in others. Currently using 4.6.2, and have tried using the type canvas and gameobject. Canvas type works fine in other scenes.

This is the error message:
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.

Enabling the canvas in the editor works fine, so it doesn’t appear to destroyed. Clicking on the reference field in the controlling script also highlights the correct canvas. However, a if(canvas == null) does show the canvas / it’s gameobject as being null.

Am open the the possibility that it’s a bug that has been fixed, but due to another project, I’d prefer to update (either to a later version of 4, or 5) as a last resort. Any suggestions? Thanks for reading!

Update 1: I tried setting up the scene with all the canvases active, and then disabled the ones which aren’t used in the setup methods. No problems with null pointers this way. I was even able to SetActive(true) the canvas immediately after doing this, but trying to set the canvas active later doesn’t work.

How are you referencing the UI objects? I have problem referencing inactive UI objects with:

GameObject.Find();

But it will work if I use the below and drag the object in the editor to assign it (active or inactive):

public GameObject canvas;

A disabled game object is not treated as destroyed. Your problem is with the Awake and Start methods not being executed for disabled game objects (it’s not a bug, it’s the expected behaviour), and you’re probably accesing something that’s only set in one of those methods. That’s why everything works when you keep everything active and deactive them with a script, and doesn’t work then you deactivate them in the inspector.

What solved the problem in this case was a little unusual, but may as well describe what happened in the event it’s of use to someone else. I found out that the reference to certain canvases was being lost at some point during execution.

This wasn’t happening straight away. The canvases were available, and could be enabled and disabled at will, through code, up until I ran the logic to compile the data required for a level debriefing. So, started tracking back, searching for the point at which the canvas references were lost. This happened when I sent a request to another script (GameData) to collect the data required for the debriefing.

Rather than calling the method to display the canvas directly from within the debriefing data collection method in GameData, I changed it to return a debriefing data object back to UI script, then called the method to display the canvas from the UIScript. I’m a little baffled that this changed anything, but it is working now.

I just ran into a similar problem and I wanted to post my solution. I am convinced this is a bug. Unity 5.1.1f1.

My problem was that I had an array of custom classes, with each class having a reference to a gameobject assigned in the scene. I then later decided I wanted the Gameobject references to point to a CanvasGroup on their object instead. I added CanvasGroups to the gameobjects and then changed the reference lookups in code from Gameobject to Canvasgroup.

Everything looked fine in the inspector and compiled fine, but as soon as I ran one of my first lines of code: “line.m_Pivot.transform.position = m_LineStartZeroAlphaPos.position;”

(Note: m_Pivot is the reference to the CanvasGroup)
It would crash with the error: "The object of type ‘CanvasGroup’ has been destroyed but you are still trying to access it.

No matter how many times I ran the code / enabled the object/ rebooted Unity, I would get the same error. My “fix” finally came from commenting out all references to the object in code, so that the inspector would no longer display the option to hold that reference, then I commented out all the lines of code and re-hooked up the exact same references in code/scene… and then it worked.