I have a game where there are about 4 empty objects that are my object "spawners". I have two scripts attached to all of them, one that will keep spawning objects from "basictable"
, and then switch to the other component which will then spawn objects from "complextable"
.
This is my first script :
public void SpawnA() {
if (destroyer.spawncount < destroyer.spawnchange) { //if condition
int randomi = Random.Range (0, basictable.Length);
Instantiate (basictable[randomi], new Vector3 (this.transform.position.x,this.transform.position.y,basictable[randomi].transform.position.z), Quaternion.identity);
Invoke ("SpawnA", Random.Range (mini, maxi));
}
else if (keep){
this.GetComponent<spawnscript2>().SpawnB();
keep = false;
}
And the second script :
public void spawnB() {
int randomi = Random.Range (0, complextable.Length);
bobo = (GameObject) Instantiate (complextable [randomi], new Vector3 (this.transform.position.x, this.transform.position.y, complextable [randomi].transform.position.z), Quaternion.identity) as GameObject;
StartCoroutine ("DelayedSpawnC");
}
IEnumerator DelayedSpawnC () {
yield return new WaitForSeconds (6);
destroyer.spawncount = 0;
this.GetComponent<basicSpawner> ().keep = true;
this.GetComponent<basicSpawner> ().SpawnA();
}
Logically, this should work. And indeed it does. But the problem I'm having is that my "spawner", sometimes works, and sometimes doesn't, some objects from the complextable are always spawned, but in other cases the spawner just does nothing, until it switches to the first script. Is there anything I can do to optimize the spawning process and avoid this? I can't get ahold of why are some blocks spawned and others take much longer, while other blocks don't get instantiated at all.