I have encountered one problem with my script. I want to make when all enemies are dead, to set 30 seconds till next wave or if you dont want to wait, start new wave when you press the button. The problem is that when I press the button everything works as it should, but when I wait 10 seconds, it spawns double of event triple the amount of enemies, I guess it loops.
Here is the part of the code, which in my opinion causes the problems :
function Update()
{
EnemiesLeft = AllEnemies.transform.childCount;
timeEnded = false;
}
function Timer(){
yield WaitForSeconds(10); // wait 10 seconds
print("time ended");
timeEnded = true;
}
function OnGUI () {
if (EnemiesLeft == 0 ){
Timer();
if (GUI.Button(Rect(x,y,80,80),btnTexture,customButton) || timeEnded == true ){
WaveManager();
}
}}
i think it was that you were calling the “TimeEnded = false;” every frame. any way i have made my own version of it, im not a java script writer so no hate. hoped it helped…
var EnemiesLeft : int;
var TimeEnded : Bool;
var 30sTimer : float;
function Start ()
{
TimeEnded = true;
30sTimer = 0f;
}
function Update()
{
//so tag all your wave enemies with a tag called "WaveEnemies" or something
EnemiesLeft = GameObject.FindGameObjectsWithTag("WaveEnemies").Length;
Timer ();
}
function Timer()
{
30sTimer += Time.deltaTime * 1;
if (30sTimer >= 30f)
{
WaveManager();
30sTimer = 0f;
}
if (EnemiesLeft == 0 )
{
yield WaitForSeconds(10); // wait 10 seconds
print("Wave Ended");
TimeEnded = true;
}
else if (EnemisLeft > 0)
{
Debug.Log("Still More Enemies + EnemiesLeft");
TimeEnded = false;
}
}
function OnGUI ()
{
if (GUI.Button(Rect(x,y,80,80),btnTexture,customButton) || TimeEnded == true )
{
WaveManager();
TimeEnded = false;
}
}
}
Hi,
Thanks for the script, but with your script I have the same problem I spawns sometimes the number of the as it should ( 8 enemies), but sometimes not and spawns a lot more enemies than it should( 16;24;32), so I think that function is called more than one time and loops somewhere
P.S It sometimes spawns a new wave even without waiting the 10 seconds.