Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JA_555 · Oct 26, 2014 at 09:03 PM · gameobjectdisableactive

How do I set a reference in the inspector

I am trying to deactivate and activate an object and it won't re-activate, so how do I set a reference so I can re-activate it?

I read through this: http://forum.unity3d.com/threads/how-to-enable-a-gameobject.10372/ but no one explained how to set a reference in the inspector. This is the corrected code from JSIERRAAKAMC to set the reference. However, my ofher script is still not enabling theObject

   var theObject : GameObject;
   function Start() {
   theObject = GameObject.Find("The Object");
  //It only needs to be deactivated once instead of every frame so that it isn't deactivated right after another script activates it
   theObject.active = false;
   theObject.camera.enabled=false;
   }
   function Update() {
   //do stuff!
  
   }

The other script is as follows

 Var object : GameObject;
 
 function OnTriggerEnter(){
 GameObject.Find("theObject").active = true;
 }


Comment
Add comment · Show 4
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 Itaros · Oct 26, 2014 at 10:05 PM 0
Share

Ehm. What is the problem? http://docs.unity3d.com/$$anonymous$$anual/Inspector.html

avatar image JSierraAKAMC · Oct 26, 2014 at 11:33 PM 0
Share

Why are you disabling it every frame? This could be why it's not "re-activating." To fix this, move the "theObject.active = false;" and "theObject.camera.enabled=false;" to Start() that way it'll be deactivated but can be reactivated. The corrected code would look like this:

  var theObject : GameObject;
  function Start() {
  theObject = GameObject.Find("The Object");
 //It only needs to be deactivated once ins$$anonymous$$d of every frame so that it isn't deactivated right after another script activates it
  theObject.active = false;
  theObject.camera.enabled=false;
  }
  function Update() {
  //do stuff!
 
  }
avatar image MrSoad · Oct 26, 2014 at 11:41 PM 1
Share

JSierra is right, also you are using :

 var theObject : GameObject;

Which means this should show up in the inspector, so you should be able to drag and drop the object that you want into this var here.

If you had put "private" before it then it would not show in the inspector and you could not do this. So you don't need to write this :

 theObject = GameObject.Find("The Object");

code to find it at run time. Or if you would rather keep your inspector clear and find it with code than write "private var".

Both of you might want to format your code better too, you will find it far easier to read and get a lot less errors and problems as a result. Something more like this :

 var theObject : GameObject;
 
 function Start() {
 
     theObject = GameObject.Find("The Object");
     theObject.active = false;
     theObject.camera.enabled=false;
 }
 
 function Update() {
 
     //do stuff!
 }
avatar image JA_555 · Oct 27, 2014 at 08:18 PM 0
Share

@Itaros Ehm. The problem is that it is not re-activating: http://answers.unity3d.com/questions/817917/how-do-i-set-a-reference-in-the-inspector.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by christophfranke123 · Oct 28, 2014 at 09:14 AM

GameObject.Find("...") does not return inactive game objects. So if you want to activate the game object, you will need to store a reference of it (possibly in the Awake() function). See GameObject.Find()

If you only want to hide your object from screen, I would recommend to use

 gameObject.renderer.enabled = false;

Also note, that you deactivate a game object called "The Object" and then you want to activate "theObject". This looks like a typo.

If the game object has no camera attached (that means it is not a camera), gameObject.camera will be null and

 gameObject.camera.enabled = false

will throw an exception. Every line after that line in your function will not be executed then.

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to active/deactive gameobject? 1 Answer

How to disable gameobjects in code? 4 Answers

How to make an open and close box?,How to make open and close a box? 0 Answers

If you disable a gameobject, does an InvokeRepeating loop end or pause? 3 Answers

GameObject.setActive(bool) is not activating my gameObject 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