- Home /
Two Times Button - Looping
Hello!
I have a small problem with looping the if/else function.
I need to turn off/on the lights in my game, while pressing the Middle Mouse Button. So I created animation for my lights and simply play it with normal speed, when the lights are off, and with inverse speed, when they're turning on.
I wrote a small code for twice pressing the Middle Mouse Button (the example of twice pressing code I found on this site), but everything works ONLY ONCE. I press the button - the lights are turned off, press it again - they are turned on. Press again - and nothing going on! So I think, maybe it is possible to loop the function?
Please, help me and sorry for my English.)
Here's the code:
var LampsOn = 0;
for (var state : AnimationState in animation)
{
state.speed = 0.0;
}
function Update() {
if (LampsOn == 0 && Input.GetMouseButtonDown(2))
{
for (var state : AnimationState in animation)
{
state.speed = 1.0;
};
LampsOn = 1;
}
else if (LampsOn == 1 && Input.GetMouseButtonDown(2))
{
for (var state : AnimationState in animation)
{
state.speed = -2.0;
};
LampsOn = 0;
}
}
Do you have to have all that whitespace in there? It makes reading your script really tiring.
Sorry. Seems, now i fixed it. (Fixed the code - not the problem!)
Answer by Rotarus · Nov 24, 2011 at 02:02 PM
Perhaps, the problem is linked with state.speed, because in all other cases the looping method, which I found in unity answers (see the link below) works great.
http://answers.unity3d.com/questions/21584/button-pressed-twice-conversation-text-skip.html
Your answer
Follow this Question
Related Questions
Run a function aslong as a button is pressed 3 Answers
How do I repeat a part of my script? 2 Answers
Create GUI based on an array 2 Answers