x


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!!!

more ▼

asked Feb 02 '12 at 05:16 AM

POLYGAMe gravatar image

POLYGAMe
432 37 46 53

It's the bit in the WHILE TRUE loop that is giving me trouble, the damage gauge works well.

Feb 02 '12 at 05:17 AM POLYGAMe

Oh, before I forget, I also had the exact same issue when I tried this with a GUIText, rather than a GUITexture object...

Feb 02 '12 at 05:20 AM POLYGAMe

If you're calling that in Update, it's making new instances of the coroutine every frame, which would account for the erratic behavior.

Feb 02 '12 at 05:25 AM Eric5h5

Ah, I thought it might be something like that... but how do I call it without being called from Update? It needs to be checking constantly throughout gameplay... and the other text I have is being called from Update and that works... so weird...

Feb 02 '12 at 05:28 AM POLYGAMe

Create a function (FlashTex or something) with a while( true ). Put your flashing code inside an if scope, with a boolean (let's say bFlash). Start your coroutine on awake, then toggle bFlash if you need the texture to flash or not. I hope I'm making sense !

Feb 02 '12 at 06:04 AM Berenger
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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.

more ▼

answered Feb 02 '12 at 03:42 PM

Berenger gravatar image

Berenger
11k 12 19 53

Thanks, only just saw this. Accepted and up-voted :)

Feb 13 '12 at 05:07 AM POLYGAMe

Oh, by the way, it was originally bTextIsFlashing... but I got deeper and deeper into crap and ended up changing it for some reason... can't remember why but it's back to what it was originally though anyway. I remember thinking that the Unity Answers guys would think that was an odd name for a bool! LOL.

Feb 13 '12 at 05:08 AM POLYGAMe
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x475
x170
x16

asked: Feb 02 '12 at 05:16 AM

Seen: 725 times

Last Updated: Feb 13 '12 at 05:08 AM