Hi wonder if anyone can help please i am trying to spwan objects in random locations from within a coroutine and then when timer ends start it over again, the code works for the first time but after that it spawns the object twice in different locations with half a second of each other then on each subsequent call to spawn it increases and i only want the one.
here is the code for spawn:
public IEnumerator Spawn()
{
spawnTimer = 60;
endGame = false;
while(spawnTimer >0 && endGame == false)
{
spawnX = Random.Range(-15, 15);
spawnZ = Random.Range(-15, 15);
Vector3 spawnPos = new Vector3(spawnX, 15f, spawnZ);
transform.position = spawnPos;
int i = Random.Range(0,3);
GameObject clone = Instantiate (spawnCubes *, transform.position, Quaternion.identity) as GameObject;*
-
yield return new WaitForSeconds(delay);*
-
}*
-
}*
i am checking the timer from within update
void Update () -
{*
-
if (spawnTimer <= 0)*
-
{ *
-
endGame = true;*
-
spawnTimer = 0;*
-
StopCoroutine(Spawn());*
-
NewLevel();*
-
}*
-
spawnTimer -= Time.deltaTime;*
-
}*
which then calls new level
public void NewLevel() -
{*
-
boxDestroyer ();*
-
TagSetter ();*
-
delay--;*
-
StartCoroutine (Spawn ());*
-
}*
destroy the objects and tags with
public void boxDestroyer()
-
{*
-
GameObject[] killCube = GameObject.FindGameObjectsWithTag ("Cubes");*
-
foreach (GameObject die in killCube)*
-
{*
-
Destroy(die);*
-
}*
-
GameObject[] killCollect = GameObject.FindGameObjectsWithTag ("Collect");*
-
foreach (GameObject die in killCollect)*
-
{*
-
Destroy(die);*
-
}*
-
}*
resets the tags with
public void TagSetter() -
{*
-
spawnWho = Random.Range (0, 3);*
-
for (int i =0; i<spawnCubes.Length; i++)*
-
{*
-
if(i == spawnWho)*
-
{*
_ spawnCubes*.tag = “Collect”;_
_ collectMe = spawnCubes.name;
}
else*
spawnCubes*.tag = “Cubes”;
}
}*
then reduces the delay which is the time to wait for seconds until next object spawn and i call the spawn co routine again. can anyone help please i know idea what i am doing wrong._