Hi everybody!

currently, to wait in scripts, i make a float variable that augments every frame, and when it reaches a threshhold, the script continues on. so if the frames per second of the game is 30:

public float WaitTime = 0.0f;
public float Threshhold = 90f;

WaitTime++;

The WaitTime will be of 3 seconds before it reaches the threshhold. I am sure this has a huge hit on performance if you have hundreds of scripts using this technique.

I researched on the IEnumerator and YieldForSeconds thing, but cannot seem to comprehend it. Is there any simple way to wait an amount of time in Cscripts or can somone explain in detail IEenumerator and YieldForSecond functions for a noob? thank you very much :slight_smile:

if anyone is still unsure of what I am asking, I want to know how to make my script wait a number of seconds before passing to the next line of code. Thanks!

Coroutines and Invokes are very simple to use, actually. I suggest you spend some time acquainting yourself with them. All of your projects will benefit from their use.

For instance, if you wanted something to repeat every 3 seconds you could use either a Coroutine or an Invoke.

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
	public bool useInvoke;
	public bool running;
	
	void Start()
	{
	    if (useInvoke)
	        InvokeRepeating("TestInvoke", 0, 3);
	    else
	        StartCoroutine(TestCoroutine());
	}
	
	void TestInvoke()
	{
	    Debug.Log("TestInvoke()");
	}
	
	IEnumerator TestCoroutine()
	{
		running = true;
		
	    while (running)
	    {
	        Debug.Log("TestCoroutine()");
	        yield return new WaitForSeconds(3);
	    }
	}
}

Attach that script to a gameObject and play with the useInvoke flag from the inspector if you want. Unchecking the running box in the inspector will quit the coroutine. Hopefully you can use this as a template to implement your own! Good luck!

I wanted to wait x Seconds in the Update() before continuing.
So you cannot use the yield there (don’t want to start a coroutine every frame right?)

So if someone, like me, searched this / needs this in and for the Update():

private float timer = 0;

private float timerMax = 0;

void Update ()
{
   text += "Allow yourself to see what you don’t allow yourself to see.";

   if(!Waited(3)) return;

   text += "

Press any key!";
}

private bool Waited(float seconds)
{
    timerMax = seconds;

    timer += Time.deltaTime;

    if (timer >= timerMax)
    {
        return true; //max reached - waited x - seconds
    }

    return false;
}

what about when the code is not in a void? for example:

GameObject muzzleflash = instantiate…

yield WaitForSeconds (0.4f);
Destroy (muzzleflash);

this code above don’t work. how could I do lines of code that simple without create a new void?

I tried to use IEnumerator like this;

GameObject muzzleflash = instantiate…

Wait (0.4f);
Destroy (muzzleflash);

IEnumerator Wait (float amount) {
yield return new WaitForSeconds (amount);
}

please tell me what I am doing wrong.