Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Delaying

Discussion in 'Scripting' started by UnityUser_721, Aug 11, 2014.

  1. UnityUser_721

    UnityUser_721

    Joined:
    May 3, 2014
    Posts:
    19
    Is there any way to delay actions?
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Use coroutines?

    Code (CSharp):
    1. void DoSomething(){ StartCoroutine("delayedaction"); }
    2.  
    3. IEnumerator delayedaction()
    4. {
    5. yield return new WaitForSeconds(2);
    6. //do something
    7. }
    You can also just use Invoke.

    Code (CSharp):
    1.  
    2.  
    3. void DoSomething(){ Invoke("delayedaction", 2); }
    4.  
    5. void delayedaction{
    6. //do something
    7. }
     
  3. UnityUser_721

    UnityUser_721

    Joined:
    May 3, 2014
    Posts:
    19
    Hmmmmm...ok.
    I wish there was an easier way like delay(1) or sleep(1000).
     
  4. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    maybe they will add a delay function in the future...but for now coroutines and Invokes with waitForSeconds work just as good..plenty of examples in these forums...just search
     
  5. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    That would lock the thread the game is running on. The game loop is a single thread and that's partly why you need coroutines or Invoke.