wait function can't work! need help pls

hi guys, I wanted to my function to wait for a few seconds before removing but somehow can’t work. Heres my code.

IEnumerator wait(int seconds) {
yield return new WaitForSeconds(seconds);
}
void Update () {
wait(5);
gEleCollection.Remove(gEleCollection[0]);
}

somehow it didn’t wait and just go to the next line.

Update gets run every frame. So it calls wait every frame.

Also, you can’t pause Update.

float timer;

IEnumerator SetTimer( int seconds ) {
  timer = Time.time + seconds;
}

void Update() {
  if ( Time.time >= timer  timer != 0 ) {
    gElewhatever
    timer = 0.0;
  }
}

Thx alot Vicenti, I tried it but i got error saying that SetTimer function does not return anything so i changed it to
IEnumerator SetTimer( int seconds ) {
yield return timer = Time.time + seconds;
}

Then no more error but it still didn’t wait and instead freezes my application.

Ach, my bad. The function should be void SetTimer, not IEnumerator SetTimer.

Call SetTimer from anywhere and it’ll directly set the timer, so you could add

function Start() {
  SetTimer( 5 );
}

And after five seconds it’ll run the glElewhatever line.

i tried that but i can’t yield return timer = Time.time + seconds; because void is not an iterator interface type.

ok i removed the yield return it worked. thx alot vicenti. great help :slight_smile: