I'm testing out a script where by when the player presses a key "1" on the keyboard, it will disable a component. The script is as follow.
var c1 : GameObject;
var c1Boo : boolean = false;
function Update()
{
if(Input.GetKey("1")
{
if(!c1Boo)
{
c1Boo = true;
CheckBoo();
}
else if (c1Boo)
{
c1Boo = false;
CheckBoo();
}
}
}
function CheckBoo()
{
if(c1Boo)
{
c1.GetComponent("MoveCube").enabled = false;
}
else if(!c1Boo)
{
c1.GetComponent("MoveCube").enabled = true;
}
}
Correct me if my logic is wrong. In my Update(), if the player presses the key "1", it will check for the first condition which the c1Boo is false. Then it will turn c1Boo to true and activate CheckBoo() which will check for c1Boo condition. If that condition is true, c1 will be disabled. The CheckBoo should only run when the player presses "1". But I did a Debug.Log() and it shows that the CheckBoo is running for a few more frames before it stops.
When I used an OnGUI(), making the control a button, I don't face this problem. The gameObject is disabled when the player first clicks it and will be enabled when the player clicks again.