Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Feb 08, 2017 at 01:08 AM by Joeltron.
avatar image
0
Question by Joeltron · Jan 16, 2017 at 01:42 PM · scriping

Unity ''forgets'' object reference after object has been disabled, and will not reenable it

I have an object (Canvas_Level) that is present and active in the scene, I have a gameManager object that sets it disabled (Game_Level.SetActive(false), and sets another object (Canvas_Map) enabled. (Canvas_Map.SetActive(true);

Each object is a different UI that needs to be displayed at different times. I have a button that is supposed to switch which UI is shown. It does this by setting the onbect either active or not.

 void Start () {
         canvasMap = GameObject.Find ("Canvas_Map");
         canvasLevel = GameObject.Find ("Canvas_Level");
         playerCharacter = GameObject.Find ("Player");
         mapCamera = GameObject.Find ("MapCamera");
         ActivateMap ();
         
     }
     void ActivateMap(){
         canvasLevel.SetActive (false);
         playerCharacter.SetActive (false);
         mapCamera.SetActive (true);
         canvasMap.SetActive (true);
         MapActive = true;
     }
     void ActivateLevel(){
         MapActive = false;
         mapCamera.SetActive (false);
         canvasMap.SetActive (false);
         canvasLevel.SetActive (true);
         playerCharacter.SetActive (true);
 
     }


(there is also a method, linked to a button, that calls the ActivateLevel() method.

However, after the objects have been disabled by the script, unity "forgets" the references to them is throws an error when i try reenable them.

Comment
Add comment · Show 2
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 NoseKills · Jan 16, 2017 at 02:47 PM 0
Share

An object reference can be null because A) it hasn't been assigned B) it's a Unity object that has been Destroyed (or lost in scene loading) C) null has been assigned as its value

You have to find out which of these is the case.

Calling SetActive(false) on a gameObject will definitely not null any references to it, but if you disable a GameObject and then call your Start() method again, for example, Unity won't find the disabled objects and will assign a null to those variables.

avatar image roman_sedition · Jan 16, 2017 at 02:59 PM 0
Share

Are you using the game$$anonymous$$anager to do ActivateLevel() or are you using some other class which has no references to call it?

So far it only looks like your game$$anonymous$$anager has references and then it turns them all off, depending on the order the scene loads your other objects won't be able to find them if the game$$anonymous$$anager has turned them off. But they can just find your game$$anonymous$$anager and use the game$$anonymous$$anager's references.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody help 0 Answers

Can't reactivate object 1 Answer

Error BCE0019 1 Answer

Invalid SceneHandle 0 Answers

Player using turret problem 1 Answer


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