Search Unity

How do i count clones spawned???

Discussion in 'Scripting' started by UnanchoredStudios, Feb 10, 2020.

  1. UnanchoredStudios

    UnanchoredStudios

    Joined:
    Jan 26, 2020
    Posts:
    6
    public class Spawner : MonoBehaviour
    {
    public GameObject[] enemies = new GameObject[0];
    public GameObject[] ecliption = new GameObject[0];
    public Vector3 spawnValues;
    public float spawnWait;
    public float spawnMostWait;
    public float spawnLeastWait;
    public int startWait;
    public bool stop;
    int randEnemy;

    void Start()
    {
    StartCoroutine(waitSpawner());
    }
    void Update()
    {
    spawnWait = Random.Range(spawnLeastWait, spawnMostWait);
    }

    IEnumerator waitSpawner()
    {
    yield return new WaitForSeconds(startWait);

    while(!stop)
    {
    randEnemy = Random.Range(0, 2);

    Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));

    Instantiate(enemies[randEnemy], spawnPosition + transform.TransformPoint (0, 0, 0), gameObject.transform.rotation);

    yield return new WaitForSeconds(spawnWait);

    }

    if(enemies.Length > 9 && ecliption.Length < 70)
    {
    stop = true;

    randEnemy = Random.Range(0, 2);

    Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));

    Instantiate(ecliption[randEnemy], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);

    yield return new WaitForSeconds(spawnWait);
    }
    }
    }

    Hello, alot of posts have spoken about CancelInvoke to stop the clones spawning but I need help working out how to keep track of the amount of enemy's have spawned so that when a certain amount have been spawned the function stops and then a different enemy called the ecliption in this instance can be spawned instead.

    Any advice would be greatly appreciated :)
     

    Attached Files:

  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Please use code tags.

    You should add the spawned enemies to your list, and stop spawning if the List contains enough enemies. Don't Forget to remove enemies when they despawn.
     
  3. DitchTurtL

    DitchTurtL

    Joined:
    May 23, 2017
    Posts:
    10
    You should probably double check some of your logic before looking for a quick fix to this.

    line 51 in your file tries to stop generating enemies and generate the ecliption by seeing if enemies.Length > 9.....

    The problem is, line 14 is the only time you add anything to the enemies array. So, if you only had 2 enemies when the Spawner script is loaded, you'll generate enemies forever. If you had 20 enemies, you'd generate zero new, but you'd generate 1 ecliption unless there were 70 others already generated.

    Like I say, there are some general faults with the code that should be tightened up first, but generally on line 51, you want to replace enemies.Length with a value that's being updated when new objects are being created. Simplest way would be to move the enemies array into the Update, but, that's pretty ugly, so add objects to the array as you're creating them, keep track of them there instead of EVER using FindGameObjectsWithTag if you can help it.