- Home /
Answer by code-blep · Aug 09, 2012 at 08:55 PM
Hi! this is my first answer so please be gentle with me people ;)
function Update()
{
whatever = false;
yield WaitForSeconds (2);
whatever = true;
}
Hope it helps :)
Paul
Congrats on that small milestone :)
Well two things.
You are missing the opening and closing bracket after function Update. It should be like this: "function Update ()". but that doesn't really matter that much.
And you are using yield in Update which is a function that cannot be a coroutine.
So you should create a new function and call that from the Update function. But even when doing so the script properly wouldn't work as expected anyway.
Curse my eagerness to help. We will use my example on what NOT to do. Thank you for correcting me OrangeLightning. I'll up vote yours...
I've updated the brackets for what it is worth...
Wow, much appreciated. Do not worry we have all been there :)
function Update() { whatever = false; lefunction();
}
function lefunction(){ yield WaitForSeconds (2); whatever = true; yield WaitForSeconds (2); }
here
Ahh... That is pretty much the same thing. Although you do call a function that can be a coroutine, but this will not switch a bool every two seconds. It will wait two seconds and the it will be true forever.
Answer by FLASHDENMARK · Aug 09, 2012 at 08:56 PM
var thatBoolean : boolean;
function Awake (){
InvokeRepeating("Switch", 0, 2);
}
function Switch (){
thatBoolean = !thatBoolean;
}
There you go.
Answer by Lachee1 · Aug 10, 2012 at 12:16 AM
var isToggling : boolean = false;
var ToggleOnOff : boolean = false;
function Update(){
if (!isToggling){
Toggle();
}
}
function Toggle(){
isToggling = true;
ToggleOnOff = true;
yield WaitforSeconds(2);
ToggleOnOff = false;
yield WaitforSeconds(2);
isToggling = false;
}
That will toggle it on, wait for 2 secs, toggle it off, wait 2 secs more, then loops