- Home /
Problem with toggling GameObject
Hello.
So I am trying to write a javascript file in which the user can press the F12 key to show/hide a GameObject. In this case, the GameObject starts out as being shown, however I would like to make it so when the player presses the F12 key once the GameObject won't be visible (or hidden), then when the GameObject is hidden, they should be able to press the F12 key again to show the object.
Here is my code:
ToggleGUI.js
#pragma strict
var show : boolean = true;
var target : GameObject;
function Update () {
// Hides the GUI
if(show == true){
if(Input.GetKeyDown("f12")){
target.SetActive(false);
show = false;
}
}
// Shows the GUI
if(show == false){
if(Input.GetKeyDown("f12")){
target.SetActive(true);
show = true;
}
}
}
I was doing trial and error earlier and noticed some really odd things...
If I change the second if under the comment
// Shows the GUI
, I set the Input.KeyDown to ("r") or some other random key, it will show the GameObject, and will work properly but not the way I wanted. I wanted it to be hidden then shown as many times as the player wants with the same key, in this case being the F12.If I delete the
show=false;
and theshow=true;
under the if's, the first if will happen, but the second won't and I know this is because the value of show is not being changed to make the second if happen... However with this code above with the changing of the variable show, neither if statement will run, not even the first one....
Yes, I have targeted my GameObject (called GUI Holder) through the inspector.
Is it even possible to show or hide a GameObject using a single key? Let me say this, I am very new to Unity and scripting, only been in unity about two weeks now, scripting for maybe a week if that, so please take it easy on me. If all possible, I would like code similar in nature to mine, or code that is easy to comprehend.
Please help!!
-Matthew
Answer by mattyman174 · Feb 23, 2014 at 07:15 AM
if(Input.GetKeyDown(KeyCode.F12))
{
target.SetActive(show);
show = !show; // This reverses its current state
}
Theres no need to check if "show" is true or false to begin with either.
Wow... I'm so new to this... Thank you so much! Worked beautifully like a charm. Greatly appreciated.
Answer by highpockets · Feb 23, 2014 at 07:21 AM
You are setting show to false and right afterwards, you are checking if it is false and since the key was down on that frame, it will set show back to true all in the same update..
What you need to do is:
ToggleGUI.js
#pragma strict
var show : boolean = true;
var target : GameObject;
function Update ()
{
if(Input.GetKeyDown("f12"))
{
if( show == true )
{
target.SetActive(false);
show = false;
}
else
{
target.SetActive(true);
show = true;
}
}
}
Your code worked perfectly as well, but mattyman174 answered first, and I used his code before I saw your answer. Thanks though for your answer! I'll keep it in $$anonymous$$d in case I have a similar issue where your code would work better. So thanks to you as well!