- Home /
Question by
UnityGuy1243 · Sep 08, 2011 at 11:14 PM ·
loopyield
Looping a Boolean off and on
How do I make a loop that turns a boolean off and on every 5 seconds? I don't understand Coroutines and invoking.
Comment
Best Answer
Answer by aldonaletto · Sep 08, 2011 at 11:39 PM
You can use InvokeRepeating like this:
var boolVar: boolean = false;
function Toggle(){ boolVar = !boolVar; // toggles the variable boolVar }
function Start(){ InvokeRepeating("Toggle", 5, 5); // This instruction will call the function specified after an // initial delay (1st parameter) each 5 seconds (2nd parameter) }
Answer by Raimi · Aug 11, 2014 at 09:21 PM
This will give the desired effect..
private bool isOn;
void Start()
{
InvokeRepeating("OnAndOff", 0, 5);
}
void OnAndOff()
{
if(isOn){
isOn = false;
}else{
isOn = true;
}
}
Answer by DaveA · Sep 08, 2011 at 11:39 PM
Sample here: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.InvokeRepeating.html