unity wait function code example

Example 1: Time delay C# unity

void start()
StartCoroutine(Text());

IEnumerator Text()  //  <-  its a standalone method
{
	Debug.Log("Hello")
    yield return new WaitForSeconds(3)
    Debug.Log("ByeBye")
}

Example 2: how to make a method wait in unity

public float timeLeft = 30.0f

void Update()
{
  timeLeft -= Time.deltaTime;
            if (timeLeft < 0)
            {
            //Do your method here
            }
}

Example 3: wait function python

import time
#Waits 1 second
time.sleep(1)

Example 4: wait time in unity

using System.Collections;

private void Start()
{
	StartCoroutine(Wait());
}

IEnumerator Wait()
{
	//To wait, type this:
  
  	//Stuff before waiting
	yield return new WaitForSeconds(/*number of seconds*/);
  	//Stuff after waiting.
}

Example 5: unity c# run a command then wait

IEnumerator placeholder1() // weird syntax for this but its needed.
{
  COODE HERE
    yield return new WaitForSeconds(SECONDSHERE); //this is were the wait will happen. so position it in your func where you want to wait.
  CODE HERE
} // if this returns errors its most likely because you need to include the  yield return new WaitForSeconds
// if this still return errir check that you didnt clean up the tags at the top because you need the two using System.Collections; and using System.Collections.Generic; tags. So if you got rid of them just add them back.


//now we need to call the func in a void or a public void if we want to acces it outside our script.
public void placeholder2() //this can be called using a button or another scipt etc.
{
  StartCoroutine(placeholder1())
}

Example 6: wait for a second unity

void Start()
    {
        //Start the coroutine we define below named ExampleCoroutine.
        StartCoroutine(ExampleCoroutine());
    }

    IEnumerator ExampleCoroutine()
    {
        //Print the time of when the function is first called.
        Debug.Log("Started Coroutine at timestamp : " + Time.time);

        //yield on a new YieldInstruction that waits for 5 seconds.
        yield return new WaitForSeconds(5);

        //After we have waited 5 seconds print the time again.
        Debug.Log("Finished Coroutine at timestamp : " + Time.time);
    }