Enemy Spawns too many times

(edit: sorry the scripting is really odd when trying to copy it here)

The enemies are continuously re-spawning, making it look like they don’t die as they stack within the hierarchy. Any idea where I messed up?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour {

    public GameObject enemyPrefab;
    public float width = 10f;
    public float height = 5f;
    private bool movingRight = false;
    private bool movingLeft = false;
    public float speed = 3f;
    private float xmax;
    private float xmin;
    private float ymax;
    private float ymin;

    // Use this for initialization
    void Start () {
        float distanceToCamera = transform.position.z - Camera.main.transform.position.z;
        Vector3 leftBoundary = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distanceToCamera));
        Vector3 rightBoundary = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distanceToCamera));
        Vector3 upBoundary = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, distanceToCamera));
        Vector3 bottomBoundary = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, 
distanceToCamera));
        xmax = rightBoundary.x;
        xmin = leftBoundary.x;
        ymax = upBoundary.y;
        ymin = bottomBoundary.y;
        SpawnEnemies();
    }

     void SpawnEnemies() {
        foreach ( Transform child in transform) {
            GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as 
GameObject;
             enemy.transform.parent = child;
        }
    }

    public void OnDrawGizmos() {
         Gizmos.DrawWireCube(transform.position, new Vector3(width, height));
    } 

    // Update is called once per frame
    void Update () {
        if (movingRight) {
             transform.position += Vector3.right * speed * Time.deltaTime;
        } else {
            transform.position += Vector3.left * speed * Time.deltaTime;
        }
        float rightEdgeOfFormation = transform.position.x + (0.5f*width);
        float leftEdgeOfFormation = transform.position.x + (-0.5f* width);
        if (leftEdgeOfFormation < xmin) {
            movingRight = true;
        } else if(rightEdgeOfFormation > xmax)
        {
            movingRight = false;
        }

        if (AllEnemiesDead()) {
             Invoke("SpawnEnemies", 2); } {
            Debug.Log("Empty Formation");
            SpawnEnemies();
        }
    }

    bool AllEnemiesDead() {
        foreach (Transform childPositionGameObject in transform) {
            if (childPositionGameObject.childCount > 0) {
             return false;
            }
        }
        return true;
    }
}

I’m not very good at reading other people’s code, but the call to AllEnemiesDead() will always return true, so I presume it will then allow more enemies to be spawned.

Brain is rather fried by the pollen at the moment, so that’s the best input I can give you :slight_smile:

1 Like

I appreciate the reply regardless. It helps narrow down the problem! Thanks Chuck.

1 Like

Privacy & Terms