Flashing GameObject in Unity

user3114190 picture user3114190 · Feb 5, 2014 · Viewed 12.4k times · Source

How can I create a flashing object in Unity using SetActiveRecursively (Moment = 1 second).

My example (for changes):

public GameObject flashing_Label;
private float timer;

void Update()
{
    while(true)
    {
        flashing_Label.SetActiveRecursively(true);
        timer = Time.deltaTime;

        if(timer > 1)       
        {
            flashing_Label.SetActiveRecursively(false);
            timer = 0;        
        }   
    }
}

Answer

Alberto picture Alberto · Feb 5, 2014

Use InvokeRepeating:

public GameObject flashing_Label;

public float interval;

void Start()
{
    InvokeRepeating("FlashLabel", 0, interval);
}

void FlashLabel()
{
   if(flashing_Label.activeSelf)
      flashing_Label.SetActive(false);
   else
      flashing_Label.SetActive(true);
}