- Home /
Why my button in for loop isn't working?
Hi Guys,
I want to have my each new timer with a button created. And when it's pressed, it start to count down.
What the problem is:
In this for loop, each time a new Timer is created with message: `Debug.Log("Countdown START");` is returned instead.
I wanted the message to return ONLY when my Timer's button is pressed.
It's executing the Debug Log count down earlier one step. It isn't inside the if button section.
But I don't know why it didn't work in this for loop.
Below is my coding:
function OnGUI ()
{
GUI.Box(Rect(buttonRect), "NEW TIMER");
for (timer in PlayerTimerList)
{
if ( GUI.Button(timer.TimerBox,timer.TimerName) );
Debug.Log("Countdown START");
//run function countdown here
if (timer.TimerActive)
{
GUI.Label(timer.TimerBox,"!" + timer.TimerRemain);
}
}
}
With for loop in my function OnGUI. The button didn't react as it usually does.
if (GUI.Button(Rect(10,10,50,50),"WOOO"))
Debug.Log("Clicked the button with an image");
Please let me know where I have gone wrong in this button with for loop part, thanks!
Answer by moonLite · Nov 16, 2012 at 09:01 PM
Hi Guys!
It sounds a little to answer my own question :D
Solution remove the ; from the if GUI.Button
From this:
if ( GUI.Button(timer.TimerBox,timer.TimerName) );
To this:
if ( GUI.Button(timer.TimerBox,timer.TimerName) )
To make things easier to see, I added the {} to it,:
if ( GUI.Button(timer.TimerBox,timer.TimerName) )
{
Debug.Log("Countdown START");
}