How do I make a loop that turns a boolean off and on every 5 seconds? I don’t understand Coroutines and invoking.
3 Answers
3You 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)
}
This will give the desired effect…
private bool isOn;
void Start()
{
InvokeRepeating("OnAndOff", 0, 5);
}
void OnAndOff()
{
if(isOn){
isOn = false;
}else{
isOn = true;
}
}