- Home /
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;
}
Ehm. What is the problem? http://docs.unity3d.com/$$anonymous$$anual/Inspector.html
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!
}
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!
}
@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
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.
Your answer
Follow this Question
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