- Home /
Question is off-topic or not relevant
Why my 'if' condition is not working
Hello,
I have this script which should only work when the menuactive = true, but in fact it's completly ignoring, so the GUI menu continues to show :
void OnGUI(){
if(menuactive){
if(GUI.Button(new Rect(410, 750, 100, 26), "Tab to Hide")){
}
// when my set time is reached
if (myCounter1 >= mySetTime1) {
// show GUI Button
//BUY GOBLIN
if (GUI.Button(new Rect(410, 800, 210, 26), "1. Buy Goblin: 50 Gold")) {
myCounter1 = 0;
}
}
else{
GUI.Button(new Rect(410, 800, 210, 26), "Cooldown");
}
if (myCounter2 >= mySetTime2) {
// show GUI Button
//BUY ARCHER
if (GUI.Button(new Rect(630, 800, 210, 26), "2. Buy Archer: 60 Gold")) {
myCounter2 = 0;
}
}
else{
GUI.Button(new Rect(630, 800, 210, 26), "Cooldown");
}
// add time to counter
// use myCounter++; (same as myCounter+=1;) to count the frames - or
myCounter1 += Time.deltaTime;
myCounter2 += Time.deltaTime;
}
}
And second, How can I make my Cooldown button system to work when I press a key and not by mouse click (as is is now).
Thank you
The likely issue is whatever code you are using to set 'menuactive', not this code. Put this code on line 2:
if (!menuactive)
Debug.Log("$$anonymous$$enu Inactive");
Note you might also look to make sure you don't have this script attached to multiple objects.
Solved it. For some reason works only by using static variables. $$anonymous$$eh.. :D
Thank for the help. Now I only need to understand how can I activate that cooldown by being triggered by keys and not by mouse button.
Any ideas ?
I put it like this :
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C)){ myCounter1 += Time.deltaTime; }
Bad move, as it freezes my counter completly... Where to put it exactly ?
Solved it. I just put myCounter1 and 2 as condition in my spawn script.
Answer by robertbu · May 25, 2013 at 04:43 PM
// Check for the 'C' key press
if (Input.GetButtonDown(KeyCode.C)) {
// Start the cooldown.
}