• 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 DMCH · Jun 26, 2015 at 04:32 PM · gameobjectcanvasnullreferenceexception

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.

Comment
Add comment · Show 3
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 Cherno · Jun 26, 2015 at 04:50 PM 1
Share

You could let the canvas be enabled at all times and ins$$anonymous$$d disable it's subobjects, or put a sub-canvas between the root one and it's children and disable that.

avatar image InfernoZYB · Jun 26, 2015 at 05:02 PM 1
Share

Please post your script.

avatar image DMCH · Jun 26, 2015 at 05:13 PM 0
Share

@ Cherno: Cheers, will try this out now

@ InfernoZYB: The entire script is very big. Here's the method which is causing the trouble. Null reference is happening @ debriefingCanvas.SetActive(true);

     /* Activates the debriefing canvas, and loads all the data into it
      */
     public void ShowDebriefing(DebriefingData debriefingDataIn)
     {
         Debug.Log("UI$$anonymous$$anagerScript.ShowDebriefing. $$anonymous$$ethod called. Number of string pairs in debriefing data: " + debriefingDataIn.debriefingStrings.Count);
 
         // Enable the debriefing panel
         debriefingCanvas.SetActive(true);
 
         // Create a debriefing item for pair of debriefing strings
         for (int i = 0; i < debriefingDataIn.debriefingStrings.Count; i++)
         {
             ConfigureDebriefingItemAndAddToPanel(debriefingDataIn.debriefingStrings[i].debriefTitle, debriefingDataIn.debriefingStrings[i].debriefValue);
         }
 
         // Create debriefing events (if any)
         for (int i = 0; i < debriefingDataIn.debriefingEventStrings.Count; i++)
         {
             ConfigureDebriefingEventAndAddToPanel(debriefingDataIn.debriefingEventStrings[i]);
         }
     }

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DMCH · Jun 26, 2015 at 07:29 PM

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.

Comment
Add comment · 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
1

Answer by hnmikechan · Jun 26, 2015 at 05:25 PM

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;

Comment
Add comment · Show 6 · 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 DMCH · Jun 26, 2015 at 05:27 PM 0
Share

Hey, yes they are assigned to Canvas fields in the UI script, and then enabled with canvas.gameobject.SetActive(true). Some of them appear to work ok this way, but not all.

avatar image hnmikechan · Jun 26, 2015 at 05:41 PM 1
Share

If I know I will be setting UI components inactive or active I will do this approach and I haven't had problems accessing them whether they are active or inactive:

 public GameObject canvasGO;  //assign by dragging gameobject through editor
 public Button buttonGO;
 
 
 public void canvasOn(){
    canvasGO.SetActive(true);
 }
 
 public void canvasOff(){
    canvasGO.SetActive(true);
 }



I do get issues if I assign through the script:

 void Start(){
     canvasGO = GameObject.Find("canvas");
 }





avatar image DMCH · Jun 26, 2015 at 06:06 PM 0
Share

Hey again. Yes, am using the method you outlined, so that isn't the problem. Cheers tho

avatar image hnmikechan · Jun 26, 2015 at 06:16 PM 0
Share

Okay sorry :) Just wanted to be sure.

avatar image hnmikechan · Jun 26, 2015 at 06:19 PM 0
Share

But I do agree with the recommendation above. Rather than setting canvases active or inactive, I typically go by child Panels inside a single canvas. I understand your needs though, especially if you have separate world space and screen space canvases.

Show more comments
avatar image
1

Answer by DiegoSLTS · Jun 26, 2015 at 06:57 PM

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.

Comment
Add comment · Show 1 · 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 DMCH · Jun 26, 2015 at 07:14 PM 0
Share

Hi, thanks for info. Yes, that makes sense regarding the difference between disabled and destroyed. Even starting the scene with all canvases active and selectively disabled the ones that aren't needed through a script, the references to the canvas were still being lost at some point in the game logic. The problem is now resolved, but to be honest, I'm at a loss as to how the solution even worked.

avatar image
0

Answer by HelTroy · Jan 15, 2016 at 05:43 PM

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.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

Select every GameObject in GameObject Array 2 Answers

How to Check if a component Exists on a Gameobject. 3 Answers

How do you place an image on an object so that the object size is based on pixels per unit and image size? 1 Answer

Canvas shrinks over time 0 Answers

how can I add a 2D label to a 3D GameObject? 2 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