Search Unity

wait

Discussion in 'Scripting' started by 787gabriel777, Jul 28, 2015.

  1. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    hello
    my enemy spawn doesnt function.
    i dont now why but the time of spawn doent function!
    help me !
    Code (JavaScript):
    1. #pragma strict
    2. var Projetil : GameObject;
    3. function Start () {
    4.  
    5.  
    6. }
    7.  
    8. function Update () {
    9.     wait();
    10.     Instantiate(Projetil,transform.position,transform.rotation);
    11.  
    12. }
    13. function wait(){
    14. yield WaitForSeconds (5);
    15. }
    16.  
     
    Last edited: Jul 28, 2015
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    This is not how coroutines work. Here's how things are going to happen: every frame, Update gets run, and every frame it sees that you want to make a new coroutine called wait, so it makes a new one, every frame, until you have thousands of them all running at the same time, each one waiting 5 seconds after it's created in order to "finish" and destroy itself (and besides waiting, each one does absolutely nothing).

    Also, the Update function will keep going- because making a new coroutine won't pause the Update function, so it'll run Instantiate on the prefab every frame too. If you want to only run a coroutine once with no trigger, then put the "wait()" in the Start function instead. If you want the timing of Instantiate to be based on the coroutine, then you need to put the Instantiate inside the coroutine, like this:
    Code (csharp):
    1. function Start()
    2. {
    3.     wait();
    4. }
    5.  
    6. function wait()
    7. {
    8.     yield WaitForSeconds(5);
    9.     Instantiate(Projetil,transform.position,transform.rotation);
    10. }
    If you want to loop the Instantiation call every 5 seconds, then you can loop it inside of the coroutine like so:
    Code (csharp):
    1. function wait()
    2. {
    3.     while(true)
    4.     {
    5.         yield WaitForSeconds(5);
    6.         Instantiate(Projetil,transform.position,transform.rotation);
    7.     }
    8. }
     
  3. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    thanks
    this is my script
    Code (JavaScript):
    1. #pragma strict
    2. var Projetil : GameObject;
    3. var counter : int;
    4. function Start()
    5. {
    6. wait();
    7. }
    8. function Update () {
    9. }
    10. function wait()
    11. {
    12.        yield WaitForSeconds(120);
    13.        Instantiate(Projetil,transform.position,transform.rotation);
    14.        wait1();
    15. }
    16. function wait1()
    17. {
    18.     yield WaitForSeconds(120);
    19.     Instantiate(Projetil,transform.position,transform.rotation);
    20.     pass();
    21.  
    22. }
    23. function pass(){
    24. wait();
    25. }
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    If you're just making this as an example, that's fine, but you can also just use a counter loop to do the instantiate twice before ending, or passing, or w/e- like so:
    Code (Javascript):
    1. function wait()
    2. {
    3.     for(int i = 0; i < 2; i++)
    4.     {
    5.         yield WaitForSeconds(120);
    6.         Instantiate(Projetil,transform.position,transform.rotation);
    7.     }
    8. }
    Creating coroutines from other coroutines is fine- I do it pretty often, but making duplicates doesn't serve much purpose. wait() can also just call wait() at the end of its own function directly I think- no need for an intermediary, but if you're going to do that you just should just loop like this instead.
     
    Last edited: Jul 28, 2015
  5. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    really
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Absolutely. Coroutines are timed and they REQUIRE a return value (yield) be present at some point inside. That being the case, a Coroutine calling itself (or rather, cloning itself) will not necessarily loop into infinity until it fills the memory and crashes the program the way a normal function doing the same thing would. It can certain make clones faster than they die (resulting in the same thing), easily even, but if it clones itself at the end of its sequence, there will never be more than 1 (at least, not for longer than that single frame of instantiation).

    That said, and as I said before, this isn't really serving a purpose here, I just wanted to cut down on the redundancy in the script he posted.
     
  7. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    oh, it's unityscript, you may be right that it actually invokes coroutine under the hood, but i'm probably not going to verify this now :)
     
  8. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Yep, in UnityScript, coroutines are invoked just by calling them like normal functions, and they know that they're coroutines automatically by the presence of the "yield" at some point inside of them- you don't need to explicitly define anything. It's one of the many reasons that I absolutely can't stand the language and never use it in my own code. StartCoroutine("coroutine"); and StopCoroutine("coroutine"); might seem cumbersome, but a show of explicit intent is important IMO. I cried the day they decided to allow that "var" garbage in C#.
     
    r618 likes this.
  9. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65