Unity Community


Results 1 to 12 of 12

  1. Posts
    24

    yield WaitForSeconds Not Working?

    Hi, sorry again for my noobish question but im not sure why it is not working. Basically im on walkerboystudio lab 1:

    http://www.walkerboystudio.com/html/...rse_lab_1.html

    i've finished the video but im confused what to do to finish the lab...


    First of all, this is the original code for scriptEnemy.js:

    Code:  
    1. var numberOfClicks : int = 2;
    2. var respawnWaitTime : float = 2.0;
    3. var shapeColor : Color[];
    4. var explosion : Transform;
    5. var enemyPoint : int = 1;
    6.  
    7. private var storeClicks : int = 0;
    8.  
    9. function Start ()
    10. {
    11. storeClicks = numberOfClicks;
    12. var startPosition = Vector3 (Random.Range(-6,6), Random.Range(-4,4), 0);
    13. transform.position = startPosition;
    14. RandomColor ()
    15. }
    16. function Update () {
    17.     if (numberOfClicks <= 0)
    18.     {
    19.         if (explosion)
    20.         {
    21.             Instantiate (explosion, transform.position, transform.rotation);
    22.         }
    23.         if (audio.clip)
    24.         {
    25.             audio.Play();
    26.         }
    27.             var position = Vector3 (Random.Range(-3,3),Random.Range(-3,3), 0);
    28.         WaitTime();
    29.         transform.position = position;
    30.         numberOfClicks = storeClicks;   
    31.     }
    32. }
    33.  
    34.     function WaitTime ()
    35.     {
    36.         renderer.enabled = false;
    37.         RandomColor();
    38.         yield WaitForSeconds (respawnWaitTime);
    39.         renderer.enabled = true;
    40.     }
    41.    
    42.    
    43.     function RandomColor ()
    44.     {
    45.         var newColor = Random.Range(0, shapeColor.length);
    46.         renderer.material.color = shapeColor[newColor]
    47.     }

    this script aboveis applied to the "sphere"..there's also one object called playerObject which applied with scriptPlayer.js, and here is what's inside scriptPlayer:

    Code:  
    1. var tagName : String;
    2. var rayDistance : float = 0;
    3. var score : int = 0;
    4. var gameTime : float = 5.0;
    5. var loadWaitTime : float = 3.0;
    6. var waitbefore : float = 4.0;
    7. var numberOfPointsToWin : int = 5;
    8.  
    9. function Start ()
    10. {
    11.     InvokeRepeating ("CountDown", 1.0, 1.0); // start the timer countdown
    12. }
    13.  
    14. function Update ()
    15. {
    16. {
    17.     anjing2();
    18. print ("It works!");
    19. if (Physics.Raycast (ray, hit, rayDistance))
    20. {
    21.     if (hit.transform.tag == tagName)
    22.     {
    23. var enemyScript = hit.transform.GetComponent(scriptEnemy);
    24. enemyScript.numberOfClicks -= 1;
    25. if (enemyScript.numberOfClicks == 0)
    26. {
    27. score += enemyScript.enemyPoint;
    28. }
    29.     }
    30.     else
    31. {
    32. print ("This is not an enemy!");
    33. }
    34. }
    35. }
    36. }
    37.  
    38. function CountDown ()
    39. {
    40.     if (--gameTime == 0) // gametimer will keep reducing until 0
    41.     {
    42.     CancelInvoke("CountDown")// to cancel the invoke when it reaches 0
    43.     if ( score >= numberOfPointsToWin )
    44.     {
    45.     Application.LoadLevel ("sceneScreenWin");
    46.     }
    47.     else
    48.     {
    49.         Application.LoadLevel ("sceeneScreenLose");
    50.         }
    51.     }
    52. }
    53.  
    54. function OnGUI ()
    55. {
    56. GUI.Label (Rect(10,10,100,20), "Score: " + score)
    57. GUI.Label (Rect(10,20,100,20), "Time: " + gameTime);   
    58. }
    59.  
    60.     function anjing2 ()
    61.     {
    62.         yield WaitForSeconds (waitbefore);
    63.     }

    Now they told me to do this by myself:
    Blinking Sphere

    Counter - 3 second countdown for the Sphere before disappearing
    While counting down, add a flashing element to the Sphere
    How can i do that? i have tried to add another function, similar to "WaitTime ()", i called it "WaitTime2 ()", and use differet variable name for it. I added WaitTime2(); above if explosion() in scriptEnemy.js, but it doesn't work?


  2. Location
    Tennessee, USA
    Posts
    3,452
    The top one, you are not properly calling the coroutine, you cannot just call it like a normal routine, you MUST create a seperate process for it. To do that, you use StartCoroutine...

    Code:  
    1. function Update () {
    2.     if (numberOfClicks <= 0)
    3.     {
    4.         if (explosion)
    5.         {
    6.             Instantiate (explosion, transform.position, transform.rotation);
    7.         }
    8.         if (audio.clip)
    9.         {
    10.             audio.Play();
    11.         }
    12.             var position = Vector3 (Random.Range(-3,3),Random.Range(-3,3), 0);
    13.         [COLOR="red"]StartCoroutine(WaitTime());[/COLOR]
    14.         transform.position = position;
    15.         numberOfClicks = storeClicks;   
    16.     }
    17. }

    The bottom part... This:

    InvokeRepeating ("CountDown", 1.0, 1.0);

    is not the way to do a countdown timer.

    You would start a coroutine, display 3, yield for one second, then 2, then yield for one second, then 1, then yield for one second, the let the coroutine end. At the start, you simply set Time.timeScale to zero, at the end make it 1.


  3. Posts
    22
    Hi,

    I think setting the TimeScale to zero won t let the coroutine yield for one second (or three seconds) right?


    sidev


  4. Location
    Tennessee, USA
    Posts
    3,452
    It does, because I built a countdown timer in a game that did just that.


  5. Posts
    24
    Quote Originally Posted by BigMisterB View Post
    The top one, you are not properly calling the coroutine, you cannot just call it like a normal routine, you MUST create a seperate process for it. To do that, you use StartCoroutine...

    Code:  
    1. function Update () {
    2.     if (numberOfClicks <= 0)
    3.     {
    4.         if (explosion)
    5.         {
    6.             Instantiate (explosion, transform.position, transform.rotation);
    7.         }
    8.         if (audio.clip)
    9.         {
    10.             audio.Play();
    11.         }
    12.             var position = Vector3 (Random.Range(-3,3),Random.Range(-3,3), 0);
    13.         [COLOR="red"]StartCoroutine(WaitTime());[/COLOR]
    14.         transform.position = position;
    15.         numberOfClicks = storeClicks;   
    16.     }
    17. }

    The bottom part... This:

    InvokeRepeating ("CountDown", 1.0, 1.0);

    is not the way to do a countdown timer.

    You would start a coroutine, display 3, yield for one second, then 2, then yield for one second, then 1, then yield for one second, the let the coroutine end. At the start, you simply set Time.timeScale to zero, at the end make it 1.
    ok but this wasn't my question. Even if i replace StartCoroutine(WaitTime()); with simply WaitTime();

    it won't make any difference. Atm, with this script:

    if i click the object -> it disappears -> WaitTime -> it reappears

    What i want is not like that but like this...

    if i click the object -> it waits for x seconds -> it disappear -> WaitTime -> it reappears

    now if i want this "it waits for x seconds" to be done, what to add and how?


  6. Posts
    22
    You could move the code into the Update to a CoUpdate. That is instead of void Update(){} you would put a IEnumerator CoUpdate(){}, with a while true loop inside. Then, your could do a yield return StartCoroutine instead of just a StartCoroutine. If you put yield return new WaitForSeconds(3.0) into this coroutine, then it will wait for 3 seconds before letting the code continue.
    Does that make sense ?

    Sidev



    ------------------------------------------------------------
    See "Coroutine Manager" on the Asset Store for easy and powerful coroutines !


  7. Posts
    24
    OK how to do that? You mean i put:

    IEnumerator CoUpdate()
    {
    StartCoroutine(TheWaitForXSecondsFunction());
    }

    before the if (explosion)? or how? Because it doesn't work...

    *PS: TheWaitForXSecondsFunction is the function for "it waits for x seconds", check my previous post..


  8. Posts
    22
    Hi pulento,

    I took the liberty to write you the code skeleton you should ALWAYS use for your projects.
    This should help you in solving your problems. Please let me know if something is still confusing.

    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Punleto : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.         StartCoroutine(CoUpdate()); //This is REALLY IMPORTANT
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     }
    15.    
    16.     //This is the best way to put non performance sensitive code. ALWAYS create a CoUpdate() !!! believe me.
    17.     //Note: Coroutine Manager will help you stop, pause, resume or even reset this coroutine at will! Go for it.
    18.     IEnumerator CoUpdate(){ //ALWAYS have a CoUpdate, you ll thank me later...
    19.         while (true){ //Not necessary if you are using the "loop" mode in Coroutine Manager
    20.            
    21.             /* do some of your code there */
    22.             //NOTE: this code will loop forever until you stop it based on a given condition.
    23.             print("doing some stuff...");
    24.             yield return StartCoroutine(WaitForXSeconds(3.0f));
    25.             print("now we are done waiting. Let s move on");
    26.            
    27.            
    28.             yield return 0; //Do not erase this without caution!
    29.         }
    30.     }
    31.    
    32.     IEnumerator WaitForXSeconds(float seconds){
    33.         print("starting");
    34.         yield return new WaitForSeconds(seconds);
    35.         print("done!");
    36.     }
    37. }


  9. Posts
    22
    Pulento? Are you confortable with C# instead of Javascript? There are similar in Unity but if you want to get into serious code I suggest you spend some time using it. Really.


  10. Posts
    24
    hi sidev, thanks so much for helping. I actually dont have any experience with C#. I started learning javascript since about 1 month ago, so basically im a total noob. Thanks once again


  11. Posts
    22
    Punleto,

    No problem, enjoy Coroutines

    ------------------------------------------------------------
    See "Coroutine Manager" on the Asset Store for easy and powerful coroutines !


  12. Location
    Tennessee, USA
    Posts
    3,452
    Quote Originally Posted by punleto View Post
    ok but this wasn't my question. Even if i replace StartCoroutine(WaitTime()); with simply WaitTime();

    it won't make any difference. Atm, with this script:

    if i click the object -> it disappears -> WaitTime -> it reappears

    What i want is not like that but like this...

    if i click the object -> it waits for x seconds -> it disappear -> WaitTime -> it reappears

    now if i want this "it waits for x seconds" to be done, what to add and how?
    Wha... so what you want to do is add the wait for x seconds into the wait time.... and I thought it was complicated.....

    Same script as before, just change your WaitTime coroutine to this:
    Code:  
    1.     function WaitTime ()
    2.     {
    3.         yield WaitForSeconds (respawnWaitTime);
    4.         renderer.enabled = false;
    5.         RandomColor();
    6.         yield WaitForSeconds (respawnWaitTime);
    7.         renderer.enabled = true;
    8.     }

    If you want to specify the Wait time per click.. then you would need this:
    Code:  
    1.     function WaitTime (X : float)
    2.     {
    3.         yield WaitForSeconds (X);
    4.         renderer.enabled = false;
    5.         RandomColor();
    6.         yield WaitForSeconds (respawnWaitTime);
    7.         renderer.enabled = true;
    8.     }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •