Search code examples
c#unity-game-enginengui

Flashing GameObject in Unity


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;        
        }   
    }
}

Solution

  • 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);
    }