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;
  2.  
  3. function Update () {
  4.  if(readynow){
  5.   MakeBox();
  6.  }
  7. }
  8.  
  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

37 Comments

    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){
    doSomething();
    }
    }

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

    regards,

    Stephan

    • @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’”

    Thanks

    • 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.

    Hello,

    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?

    Thanks!

    • 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.

    I’m still not sure exactly what WaitForSeconds() does. The reference link just says that it “Creates a yield instruction to wait for a given number of seconds”. From how I understand it, it should have been fine to just put the WaitForSeconds() line of code just after the instantiate line but, obviously, it wasn’t. Doesn’t it pause the executing of the lines in the script for x seconds? Would really appreciate your reply God bless

  • Awesome tutorial, however I do have a question. Once i use the
    function start()
    {
    Destroy(gameObject.Find(“Box”), 3);
    }

    than use

    function Update () {
    if(readynow){
    MakeBox();
    }
    }

    function MakeBox(){
    readynow=false;
    Instantiate(box, transform.position, transform.rotation);
    yield WaitForSeconds(2);
    readynow=true;
    }
    it dosnt work :S objects just get destroyed and don’t appear back after 3 seconds. help please!

  • Hello… I made this but it didn’t work.. it instantiate the first prefab but then it doesn’t. But I tried using another object (not a prefab) and it did work… so… what is the problem?… why I cant clone prefabs as you did?

    Thanks!!!

  • 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 () {
    if(readynow){
    MakeBox();
    }
    }

    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.

    Hi This is the C# equivalent

    using UnityEngine;
    using System.Collections;

    public class mothers : MonoBehaviour {
    public bool readynow = true;
    public GameObject box;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    if (readynow)
    StartCoroutine(MakeBox ());

    }

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

  • I always thought you could not use a coroutine in the Update function? even if it is in another function. Also even if you do find a way to use it in update function I thought its not ideal to do so and its better to avoid using any coroutine in the update function?

  • Hi I am using the same code as this tutorial and getting the error “BCE0024: The type ‘WaitForSeconds’ does not have a visible constructor that matches the argument list ‘(int)’” but just to make sure, here is mine:
    My Unity is version 4.0.1f2

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

    function Update () {
    if(readynow){
    MakeBox();
    }
    }
    function MakeBox(){
    readynow=false;
    Instantiate(box, transform.position, transform.rotation);
    yield WaitForSeconds (2);
    readynow=true;
    }

  • The C# codes would be like this:

    using UnityEngine;
    using System.Collections;

    public class creator : MonoBehaviour {

    public GameObject box;
    public bool readynow = true;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    if(readynow)
    StartCoroutine(MakeBox());
    }

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

  • Here my code for C#
    ———————————
    using UnityEngine;
    using System.Collections;

    public class myCreator : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    public GameObject box;
    bool readynow = true;

    void Update () {
    if(readynow){
    StartCoroutine( MakeBox() );
    }
    }

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

    And Thanks for all this Tuto ! Amazing Work !

    ———————————

Leave a Comment