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 /
avatar image
0
Question by blolb · Jun 29, 2016 at 05:43 PM · c#uierror

Prefab variables of disabled game objects are removed on game start.

I have two game objects InventoryCanvas and InventoryScripts. I want to be able toggle them with I press. Everything works fine if I have these objects enabled on the game start. But if they are disabled, prefabs are removed from game object that GameControl script is attached to, and I get Object reference is not set to an instance of an object error.

Would really appreciate your help. Thanks!

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class GameControl : MonoBehaviour {
     public GameObject inventoryCanvas;
     public GameObject inventoryScripts;
     private bool inventoryCanvasIsOn;
     private bool inventoryScriptsIsOn;
 
     void Start () {
         inventoryCanvas = GameObject.Find("InventoryCanvas");
         inventoryScripts = GameObject.Find("Inventory");
     }

     void Update ()
     {
         if (Input.GetKeyDown(KeyCode.I))
         {
             inventoryCanvasIsOn = !inventoryCanvasIsOn;
             inventoryScriptsIsOn = !inventoryScriptsIsOn;
             inventoryCanvas.SetActive(inventoryCanvasIsOn);
             inventoryScripts.SetActive(inventoryScriptsIsOn);
         }
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by hamado · Jun 29, 2016 at 08:09 PM

The reference error is because the script looses connection after the game object is disabled, try disabling the canvas only instead to prevent this issue.

inventroyCanvas.GetComponent().enabled = false;

Same with the other variables you are trying to toggle.

I hope this helps, good luck!.

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 blolb · Jun 30, 2016 at 10:56 AM 0
Share

Already tried that. Didn't work.

But this works fine for some reason.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class GameControl : $$anonymous$$onoBehaviour {
     public GameObject inventoryCanvas;
     public GameObject inventoryScripts;
     private bool inventoryCanvasIsOn;
     private bool inventoryScriptsIsOn;
 
     void Start () {
     }
     
     void Update ()
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.I))
         {
             inventoryCanvasIsOn = !inventoryCanvasIsOn;
             inventoryCanvas.SetActive(inventoryCanvasIsOn);
         }
         if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.I))
             {
             inventoryScriptsIsOn = !inventoryScriptsIsOn;
             inventoryScripts.SetActive(inventoryScriptsIsOn);
         }
     }
 }
avatar image
1

Answer by Bunny83 · Jun 29, 2016 at 09:26 PM

The problem is that GameObject.Find can only find active gameobjects as you can read in the docs. There is no method like GameObject.Find that can find inactive GameObjects. There is Resources.FindObjectsOfTypeAll which can find inactive objects as well, but also child objects, prefabs and internal objects of Unity.

Is there a reason why you can't assign the references in the inspector at edit time?

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 blolb · Jun 30, 2016 at 10:54 AM 0
Share

It appears that I was trying to replace references in inspector on void start. And as you said GameObject.Find can't find inactive objects.

 void Start () {
          inventoryCanvas = GameObject.Find("InventoryCanvas");
          inventoryScripts = GameObject.Find("Inventory");
      }

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.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

error CS0029: Cannot implicitly convert type 'string' to 'int'... I EVEN ADDED .ToString(); 1 Answer

Help I am getting an error CS0079 in my code and I don't know why. 1 Answer

How can I make a new line in Unity UI text? 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