Swapping textures/blinking lights

Trying to create a script that can turn textures on/off at the rate of each change tanking place every 0.25 seconds. Since you cannot use yield/coroutines in the update function what is the next best course of action?

In my mind the easiest way to do this was to create an update function like this :

function Update ()
{
	if (ButtonStates.BLINK == 1)
		{
		ButtonStates.Button_1 = 1;	
		{
	if (ButtonStates.BLINK == 0)
		{
		ButtonStates.Button_1 = 0;
		}
}

Now i just need to be able to turn BLINK on and off every 0.25 seconds. Looking around i found a few things that might work but cant seem to implement them. InkvokeRepeating?

You could use invoke repeating, yes. But you could also use a coroutine like this (untested):

function Start()
{
    Blink();
}

function Blink()
{
    while (true)
    {
        SwitchLightOn();
        yield WaitForSeconds(.25);
        SwitchLightOff();
        yield WaitForSeconds(.25);
    }
}

Indeed, in one project I actually have a blinking light routine using particles which is this:

function Blink (wait : float, wait2 : float) {
	while (true) {
		particleEmitter.emit = true;
		yield WaitForSeconds(wait);
		particleEmitter.emit = false;
		yield WaitForSeconds(wait2);
	}
}

If the on time and off time were the same, I’d use this:

function Blink () {
	particleEmitter.emit = !particleEmitter.emit;
}

Then do “InvokeRepeating(“Blink”, wait, wait);” In that case you can also use CancelInvoke at any time without having to code extra logic, which is nice. Any time you find yourself with objects that need to be in different states at different times, dump Update and use coroutines or InvokeRepeating or whatever.

–Eric

Thank you guys very much for the quick reply. My only confusion on your reply, even tho i understand it completely is… how do you set Blink to be true? or false? I have not dealt with the use of “while” before as im just learning scripting as i go currently.
Thanks again!

Edit:

Hah sorry i think i got it, i just needed to setup some type of condition, not use the words true or false. My mistake, still learning :slight_smile: