hello my enemy spawn doesnt function. i dont now why but the time of spawn doent function! help me ! Code (JavaScript): #pragma strict var Projetil : GameObject; function Start () { } function Update () { wait(); Instantiate(Projetil,transform.position,transform.rotation); } function wait(){ yield WaitForSeconds (5); }
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): function Start() { wait(); } function wait() { yield WaitForSeconds(5); Instantiate(Projetil,transform.position,transform.rotation); } If you want to loop the Instantiation call every 5 seconds, then you can loop it inside of the coroutine like so: Code (csharp): function wait() { while(true) { yield WaitForSeconds(5); Instantiate(Projetil,transform.position,transform.rotation); } }
thanks this is my script Code (JavaScript): #pragma strict var Projetil : GameObject; var counter : int; function Start() { wait(); } function Update () { } function wait() { yield WaitForSeconds(120); Instantiate(Projetil,transform.position,transform.rotation); wait1(); } function wait1() { yield WaitForSeconds(120); Instantiate(Projetil,transform.position,transform.rotation); pass(); } function pass(){ wait(); }
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): function wait() { for(int i = 0; i < 2; i++) { yield WaitForSeconds(120); Instantiate(Projetil,transform.position,transform.rotation); } } 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.
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.
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
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#.