unity 3d student

Beginner B22 – Pausing Scripts with WaitForSeconds()

Creating a pause in scripts using yield WaitForSeconds()

Code Used (Javascript)

var box : GameObject;
  1. var readynow : boolean = true;
  3. function Update () {
  4.  if(readynow){
  5.   MakeBox();
  6.  }
  7. }
  9. function MakeBox(){
  10.  readynow=false;
  11.  Instantiate(box, transform.position, transform.rotation);
  12.  yield WaitForSeconds(2);
  13.  readynow=true;
  14. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites


    What should I write if I want the javascript to pause untill a boolean is true?

  • Hello Simen,

    I’m new to Unity but I think the best way is to just use an if function for your question.

    var myBoolean : boolean;

    function Update () {
    if(myBoolean == true){

    function doSomething(){
    //here all the actions you want to perform.



    • @SEGVeenstra

      thanks, but how do I make the script pause untill a statement is true?

      • You wouldn’t make a script pause until this occasion, you’d simply place the commands you wanted into an IF, and then only execute them when a certain boolean value is true for example. like this -

        var myVar : boolean = false;

        if(myVar == true){
        //commands go here and are only triggered once something in a script changes myVar to true

        That make sense?

    That’s exactly what I needed. Thank you very much!

  • How to do this in C#?

    void MakeBox(){
    readynow = false;
    Instantiate (box, transform.position, transform.rotation);
    yield new WaitForSeconds(2);
    readynow = true;

    gives me an error “error CS1525: Unexpected symbol `new’”


    • Hi Greenthunder, no its a bit more complicated in C#, take a look at the docs using the link listed below the video ^^^ and then switch to C# using the toggle in the top right of the code view on the page.

    • If you want to use C#, you need to use IEnumerator.

      Here is example.
      IEnumerator MakeBox(){
      …. }

      and also you need StartCoroutine method, when you call MakeBox method.

      Here is example.

      void Update (){
      if( state )
      StartCoroutine( MakeBox() );


    hi could you tell me how to get unity 3d black edition?
    if you can please sen it to my email michal9034901@wp.pl and the title unity black

    • its an option if you have purchased a Unity Pro license with Unity 3.0 in preferences.

    very, very good tutorial once again, thanks

  • Awesome site and I love these tutorials! Very clear and concise AND they work! (which isn’t normally my experience with tutorials) :)

    Is there any way, however, that you could present the c# as well as the javascript? In most cases it’s not too difficult to convert the scripts but I prefer c# because I’m used to C++ and it would be nice to save the mental step.

    Unfortunately, for this script, I had to settle for using JS because I just couldn’t get a c# version of it working.

    • Hi Zenas, a few people have asked for the C# equivalent of code so I may start going through my modules and adding it soon.

    Any Python, aka “Boo” tutorials out there? Im more used to Python than C or JavaScript, so a tutorial in Boo would be really appreciated!

  • Could you post the code you use for the “Weight” in this video, or direct me to the tutorial where it was made.
    THX in advance

    • There is no code this is simply a cube primitive with a material applied to it and a rigidbody to add physics. If you’ve missed these fundamentals I recommend going back trough the videos from the start invade you’ve missed some things, good luck!

    Hi Every One I am New To Unity As Well As Javascript, Could You Please Suggest Me Which Tutorial Should i Use to learn Javascript. And One More Question Is Javascript Used In Unity is Same as The Javascript Use in HTML language?
    Because Where Ever I search For a Tutorial on Javascript It Gives me The web version Of Javascript……

  • And if i want 2 press “spacebar” and freeze the time.
    What i have 2 do? the code please!
    Thank You in advanced!!!

    • In Update(), you’d write something like – if(GetButtonUp(“Jump”)){ Time.timeScale = 0; } for example.

    HI! I´m agree with Zenas —>>> “Awesome site and I love these tutorials! Very clear and concise AND they work! ”

    but I have a little problem… yield function doesn´t works in my Unity 3D software, and without that the console shows me an error and can´t run the example. I have Unity 3D Pro 3.2.

    thanks! =)

    • ALWAYS post errors when you get them, otherwise its tricky to help! thanks

    SOLVED!! =)

  • Oh man, hahahaha.
    I really enjoyed this module.
    “quite entertaining to watch” he says, while the scene is filling up with boxes, loved the picture this gave me.

    Probably worth another game concept.

  • function Update () {

    Instantiate(box, transform.position, transform.rotation);
    yield WaitForSeconds(2);


    would this be the same of the example?

    • No, two issues here – 1. you cant put yield into Update() as Unity will have told you if you test this code. 2 – you won’t notice anything even if it did work as you haven’t put anything after the yield.


    I want to load my scene after the boss get destroyed, I got the boss to be destroyed, and a explosion to go off. How then would I script wait 5 seconds (to see the explosion) then load the scene?


    • You’d use this piece of code! write a custom function that handles instantiation of your explosion and your pause. Best thing to do is maybe write a boolean flag that you can set to true for example – like a variable called ‘gameover’ that when the boss runs out of health or whatever, call the function if gameover is false and immediately set it to true so that it only runs once.

    Hello, Can you explain to me why this works,

    var NewObject : GameObject;
    var AmountOfObjects : int = 1;
    var Timer : float = 2.0;
    function Start () {
    for (var i=0;i<AmountOfObjects;i++)
    {Instantiate(NewObject, transform.position, transform.rotation);
    yield WaitForSeconds(Timer);

    I translate this to when you press play, instantiate the object AmountofObject times while stopping the script for Timer seconds after every instantiate. Is this right?

    Howerver this doesn't seem tob e the case with this.

    var box : GameObject;
    var readynow : boolean = true;

    function Update () {

    function MakeBox(){

    Instantiate(box, transform.position, transform.rotation);
    yield WaitForSeconds(5);

    I translate this like if readynow == true, continiously instantiate while pausing the script for 5 seconds after each instantiate. This is obviously wrong since when running the script, it will instantiate infinite amount of objects without waiting for any seconds inbetween.

    Help please?

    • Yes that won’t make sense – and is an easy mistake to make – what you’re actually doing here is calling your MakeBox every frame as it is in the Update function. So regardless of your WaitForSeconds yielding.. there are still many being instantiated and waiting for 5 seconds, a new one each frame in fact! you should set readynow to false when MakeBox opens, then to true again after your yield line.

Leave a Comment