I am trying to blink my gameObject's mesh renderer 4 times with a delay of 0.25 seconds. The mesh renderer gets disabled in the beginning but never re-enables and never blinks. I thought the best way would be to use a for loop.
MeshRenderer mr;
void Start () {
mr = this.gameObject.GetComponent<MeshRenderer> ();
StartCoroutine (Blink ());
}
IEnumerator BlinkLid () {
for (int i = 0; i < 4; i ) {
mr.enabled = false;
yield return new WaitForSeconds (0.25f);
mr.enabled = true;
}
}
CodePudding user response:
You also need to wait before the next iteration!
IEnumerator BlinkLid ()
{
for (int i = 0; i < 4; i )
{
mr.enabled = false;
yield return new WaitForSeconds (0.25f); // or 0.125f if it shall take 0.25 in total
mr.enabled = true;
yield return new WaitForSeconds (0.25f); // or 0.125f if it shall take 0.25 in total
}
}
otherwise you wait to set it to enabled = true
but immeditely go to the next iteration where again you set it enabled = false
;)