hello i want to create 1 object then wait 2s and create object again in a loop. but with my code i get thousends of obects in instant . whats going on. i know its easy but still I have hard time with this. can sombody point me in right direction? Code (JavaScript): #pragma strict var kabum:boolean=false; var wybuch :GameObject; function Start () { } function Update () { if(kabum==false){ bum(); kabum=true; } if(kabum==true){ wait(); } } function bum() { var instanceBullet = Instantiate(wybuch,transform.position,transform.rotation); } function wait(){ yield WaitForSeconds(5); kabum=false;}
Don't have it in an update function. I am not familiar with UnityScript but, if you can access a method externally, I would have a method that starts a coroutine that spawns your objects.
If not, try this. var delay : float = 5.0f; var timeElapsed: float = 0.0f; function Update() { if(timeElapsed >= delay) { bum(); timeElapsed = 0.0f; } timeElapsed += Time.deltaTime; } That should work, I script in C# not UnityScript but the logic should still be the same.
the real thing is why function wait doest change kabum to false when is called ;/ function wait(){ yield WaitForSeconds(5); kabum=false;}
I am not familiar with UnityScript but, in C# you have to use StartCoroutine(wait()), perhaps you are not starting it so it never gets called. Again, you have it in the Update function so that can be causing you problems too. I suggest you create a method that starts the coroutine and instantiates whatever objects you want to be created there.
i checked i a few times and it seams like asigning new value works but...yeild waitforsecounds does not..(it seam that it work only once) so the result is that i create large number of objects in instant. I tried ti use"StartCoroutine" but no lack function wait(){ yield WaitForSeconds(5); kabum=false;} i tried also to add return kabum; but it doest work either unction wait(){ yield WaitForSeconds(5); kabum=false; return kabum;}
Code (JavaScript): if(kabum==true) {wait(); } This is faulty logic, as you will keep calling Wait function while kabum is still true (which will be the case until the first WaitForSeconds ends). Therefore, because you keep starting a new coroutine, your kabum will keep being reset to false with all those other coroutines you started!
it works now thx Code (JavaScript): #pragma strict private var kabum:boolean; var wybuch1 :GameObject; //var wybuch2 :GameObject; //var wybuch3 :GameObject; //public var lista:Array; var rr:float; function Start () { kabum=true; } function Update () { if(kabum){ bum(); } } function bum() { kabum=false ; var rr = Random.Range(0.1,1.3); var starcie1 = Instantiate(wybuch1,transform.position,transform.rotation); yield WaitForSeconds(rr); kabum=true;}