Search code examples
c#unity-game-enginewait

Waiting in Unity 3D game engine in C#?


Is there a command in Unity 3D game engine with C# to make the code wait before something happens? Here is an example:

void Start () {
    //(wait code here)
    Connect();
}

Solution

  • The promoted way of doing this with the Unity3D Game Engine tutorials is with WaitForSeconds which waits for a given float value (such as 0.5f for half a second).

    But with this rule you need to set the class to an IEnumerator so:

    public flat StartWait;
    
    IEnumerator SpawnWaves ()
        {
            //your code
            yield return new WaitForSeconds (StartWait);
        }
    

    This causes the class/object to wait without freezing the whole game logic. See also http://unity3d.com/learn/tutorials/projects/space-shooter/spawning-waves

    http://docs.unity3d.com/ScriptReference/WaitForSeconds.html