Trouble with flashing text... WaitForSeconds bad way to do it?

Hi there,

I am having issues with the following code. I want it to flash between 2 different textures… it works well for a few seconds and then starts swapping between the textures erratically. I have a similer process in another part of my game, almost identical code and it works perfectly. I have tried and tried to get this going but it’s just not working. I have tried with bools, for loops (to flash a predetermined amount of times) and nothing has worked. Is there anything obvious that I have missed?

function UpdateDamageGuage()

{

	if (iPlayerOneDamage < tDamageBarTextureArray.Length - 2)

	{

		// Change texture to match player damage

 		DamageGuage.texture = tDamageBarTextureArray[iPlayerOneDamage]; 

 	}

 	else if (iPlayerOneDamage >= (tDamageBarTextureArray.Length - 2)

 			&& iPlayerOneDamage < tDamageBarTextureArray.Length)

 	{

 		// Change texture to match player damage

 		DamageGuage.texture = tDamageBarTextureArray[iPlayerOneDamage]; 

 		

 		while(true)

		{

			DamageCritical.texture = DamageCriticalTexture;

			yield WaitForSeconds(1.0);

			DamageCritical.texture = DamageCriticalTexture2;

			yield WaitForSeconds(1.0);

		}		

 		

 	}		

	else if (iPlayerOneDamage == tDamageBarTextureArray.Length) 

	{

		bCarDestroyed = true;

		// TODO DamageCritical.texture = CarDestroyedTexture;

	}	

}

Any help gladly appreciated!!!

I belive it’s time to make it an answer. It’s completely normal that Unity doesn’t like an empty while(true), because the processor get stuck until it hit a yield (in that case). You should do that instead :

while(true)
{
    // if( bTextIsFlashing would be more logical, but I don't want to mess with your variables :p
    if( !bTextIsNotFlashing )
    {
       DamageCritical.texture = DamageCriticalTexture;
       yield WaitForSeconds(1.0);
       DamageCritical.texture = DamageCriticalTexture2;
       yield WaitForSeconds(1.0);
    }
    
    // When you yield null, it means "Wait for the next frame"
    yield null;
}

And when you change bTextIsNotFlashing to true, affect the correct texture as well.