Search Unity

coroutines and yield (i read the docs) please help

Discussion in 'Editor & General Support' started by ddfire, Nov 21, 2012.

  1. ddfire

    ddfire

    Joined:
    Dec 22, 2010
    Posts:
    53
    hi i am using unity 4.0
    i have read the docs http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html
    this is my code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class testing : MonoBehaviour {
    4.     void Start() {
    5.         print("Starting " + Time.time);
    6.         StartCoroutine(WaitAndPrint(2.0F));
    7.         print("Before WaitAndPrint Finishes " + Time.time);
    8.     }
    9.  
    10.     IEnumerator WaitAndPrint(float waitTime) {
    11.         yield return new WaitForSeconds(waitTime);
    12.         print("WaitAndPrint " + Time.time);
    13.     }
    14.  
    15. }
    16.  
    and this is my output:
    Starting 0
    UnityEngine.MonoBehaviour: print(Object)
    testing:Start() (at Assets/Sistema/Utils/testing.cs:16)

    Before WaitAndPrint Finishes 0
    UnityEngine.MonoBehaviour: print(Object)
    testing:Start() (at Assets/Sistema/Utils/testing.cs:20)

    and no more....
    why i am not getting the
    Code (csharp):
    1.  print("WaitAndPrint " + Time.time);
    output????
    thanks
     
  2. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Since I could not find anything wrong in your code, I tested it on my system and it worked fine.

    Code (csharp):
    1.  print("WaitAndPrint " + Time.time);
    printed after 2.0 seconds.

    No clue... check your console windows output again ... who knows. Attach script to an empty gameobject... if it is not printing, something is causing it but it is not the script.
     
    Last edited: Nov 21, 2012
  3. ddfire

    ddfire

    Joined:
    Dec 22, 2010
    Posts:
    53
    thanks...
    in an empty object is working.
    thanks