- Home /
Trying to get a GUI game object to disappear/reappear
I'm trying to get a GUI game object disappear when i press a button and reappear when i let go of the button. My problem is the GUI disappears, but never reappears. Here is my code:
function Update () {
if(Input.GetKeyDown(KeyCode.LeftShift))
gameObject.active = false;
if(Input.GetKeyUp(KeyCode.LeftShift))
gameObject.active = true;
}
Any help at all is appreciated! I'm pretty sure there is something small im missing here.
unity now informs me that gameobject.active in obselete and reccomends other things to use
Assets/disapear.js(3,20): BCW0012: WARNING: 'UnityEngine.GameObject.active' is obsolete. GameObject.active is obsolete. Use GameObject.SetActive(), GameObject.activeSelf or GameObject.activeInHierarchy.
Answer by Jamora · Jul 24, 2013 at 01:11 PM
You need to put the disabling/enabling script in a different gameobject to control this behaviour. The Update method doesn't run in a disabled gameobject/script. So something like
var go : GameObject;
function Update(){
go.SetActive(!Input.GetKeyDown(KeyCode.LeftShift));
}
You set the gameobject you want to control in the inspector.
i understand what you are saying, but how would i create a new game object to control the behaviour
alright thank you i was close to that i just couldn't get it working
If you just want a GameObject to disappear you can just disable the renderer.
gameObject.renderer.enabled = false;
Give it a try, the gameobject will still be active and therefor execute its update functions
this is the final script, Gey$$anonymous$$ey ins$$anonymous$$d of Get$$anonymous$$eyDown
var go : GameObject;
function Update(){
go.SetActive(!Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift));
}
Answer by Dave-Carlile · Jul 24, 2013 at 12:48 PM
Determine if the key is down, then set active to true if they key is not down.
bool keyDown = Input.GetKeyDown(KeyCode.LeftShift);
gameObject.SetActive(!keyDown);
You could also do this in one line, but not quite as clear for you...
gameObject.SetActive(!Input.GetKeyDown(KeyCode.LeftShift));
I guess this is in C# and you're doing JavaScript. The second example should be the same syntax. For the first you can do this...
var keyDown = Input.GetKeyDown(KeyCode.LeftShift);
Or something like that.
function Update () {
gameObject.SetActive(!Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift));
}
it has the same problem, it doesn't reappear i will try the first one
Why did you accept the answer if it is wrong? Jamora's answer is correct.
The answer isn't wrong. But since it didn't solve the problem directly, I accepted Jamora's in its s$$anonymous$$d.
I accepted Jamora's in its s$$anonymous$$d.
Somewhat confused about how you're accepting the answer when you didn't ask the question. :-S
With regards to this answer, it is the same logic as the code in the question, and therefore it has the same problem that Aug749 was asking about. It will never be called while the GameObject that the script is attached to is disabled, and can therefore only disable it.