Search Unity

Need help with "WaitForSeconds" script

Discussion in 'Scripting' started by Rob64, May 30, 2012.

  1. Rob64

    Rob64

    Joined:
    Aug 15, 2010
    Posts:
    18
    // Waits 5 seconds
    yield WaitForSeconds (5);
    // Prints 5.0// Prints 0
    print (Time.time);
    print (Time.time);

    I looked at the code above in the Unity Script Reference but if
    I use the line "yield WaitForSeconds" in my code below it dosn't
    execute. The code below is taken out of the book "Unity Game
    Development Essentials" and is supposed to work. It will work
    without using the yield line but I need to have a delay so as to
    be able to play the sound file before the game loads. I'm still
    learning the basics of scripting. Can anyone help me please.



    var QuitButton : boolean = false;
    var levelToLoad : String;
    var normalTexture : Texture2D;
    var rollOverTexture : Texture2D;
    var sound : AudioClip;

    function OnMouseEnter () {
    guiTexture.texture = rollOverTexture;
    }
    function OnMouseExit () {
    guiTexture.texture = normalTexture;
    }
    function OnMouseDown () {
    audio.PlayOneShot (sound);

    yield WaitForSeconds (2);

    if (QuitButton){
    Application.Quit();
    }
    else{

    Application.LoadLevel (levelToLoad) ;
    }
    }
    @script RequireComponent (AudioSource)
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    function Start()
    {
    yield WaitForSeconds(10.0);
    print("surprise!!!!");
    }


    run this code, watch your console. After 10 seconds you will see the print. Perhaps it is because you were using whole numbers with no decimal point? Regardless it is as simple as that.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Don't be afraid to use those CODE tags to format your code...

    @renman - no the decimal doesn't mean anything. Go look up implicit type casting.
    @Rob64 - what doesn't work with the code you posted?
     
  4. Rob64

    Rob64

    Joined:
    Aug 15, 2010
    Posts:
    18
    Thanks for the help, First of all I copyd and pasted renman3000's code into the top of my code under the variables section but it didn't work, then I made a separate script with just that code on its own and attached it to the object but that didn't work either, nothing was printed on the screen,
    To answer KelsoMRK, I have the script attached to buttons in a menu scene, such as play, instructions and quit, they all work ok without the yield WaitForSeconds bit of code but if I put that line in nothing happens, the screen stays on the menu scene, it won't go into the game or into the instruction scene, it just hangs at the menu scene.
     
  5. Deleted User

    Deleted User

    Guest

    I'm assuming you're using Javascript. I've only personally used this with C# but in that I've found I always had to declare the return type of the function that is called the yield return as IEnumerator. And start said function with StartCoroutine(). Not sure if Javascript is similar.

    C# Example (From memory so I apologize if I spelt any of it wrong):

    Code (csharp):
    1. private IEnumerator TestTime() {
    2.    print("Starting...");
    3.    yield return new WaitForSeconds(5.0f);
    4.    print("After 5 seconds...");
    5.    yield return null;
    6. }
    And to call it from somewhere:

    Code (csharp):
    1. StartCoroutine( TestTime() );
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    It wouldn't print in the game screen if that's where you were checking, it would be in the console.

    I didn't see any problems in your code, so I copied and pasted onto my project. It works as expected. The only change I made was instead of loading a level or quitting, it logged a message in the console.
     
  7. Rob64

    Rob64

    Joined:
    Aug 15, 2010
    Posts:
    18
    To Dman .........I can get the code to print a message in the console if I want to, but only if I comment out the yeidWaitForSeconds line.