Home > front end >  Enemies stop spawning after x amount have spawned/been destroyed?
Enemies stop spawning after x amount have spawned/been destroyed?

Time:09-17

I am very new to c# and I've come across a problem with my enemy spawner. I am making an endless top-down shooter, and after 20 enemies have been spawned and then destroyed, the enemies stop appearing. My score counter continues to increase though, which leads me to believe they're somehow being destroyed.

Here is the enemy spawn controller:

void Update()
{
    if (!spawningObject && GameController.EnemyCount < spawnSettings[0].maxObjects)
    {
        spawningObject = true;
        float pick = Random.value * totalWeight;
        int chosenIndex = 0;
        float cumulativeWeight = enemySpawnables[0].weight;

        while(pick > cumulativeWeight && chosenIndex < enemySpawnables.Count - 1)

        {
            chosenIndex  ;
            cumulativeWeight  = enemySpawnables[chosenIndex].weight;
        }

        StartCoroutine(SpawnObject(enemySpawnables[chosenIndex].type, Random.Range(spawnSettings[0].minWait / GameController.DifficultyMultiplier, spawnSettings[0].maxWait / GameController.DifficultyMultiplier)));

        Spawn();
    }
}


private IEnumerator SpawnObject(string type, float time)
{

    yield return new WaitForSeconds(time);

    
    randomSpawnPoint = Random.Range(0, enemySpawners.Length);
    randomEnemy = Random.Range(0, enemy.Length);
    enemyPool.SpawnObject(enemySpawners[randomSpawnPoint].position, transform.rotation);
    


    spawningObject = false;
    GameController.EnemyCount  ;



}

And here is the enemy controller (attached to enemy prefab):

public void Start()
{
    myRB = GetComponent<Rigidbody>();
    player = FindObjectOfType<PlayerController>();
    
   
}


private void OnEnable()
{
    player = FindObjectOfType<PlayerController>();
}

void Update()
{

 

    if (health <= 0)
    {
        Die();
    }
     
}

void FixedUpdate()
{
    transform.LookAt(player.transform.position);
    myRB.velocity = (transform.forward * moveSpeed);


    //Falling

    if(myRB.velocity.y < 0)
    {
        myRB.velocity  = Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;

    }
    
}




public void Die()
{
    print("Enemy"   this.gameObject.name   " has died!");
   
    EnemySpawn.instance.enemyPool.ReturnObject(this.gameObject);

    player.GetComponent<PlayerController>().points  = pointsToGive;

    ScoreController.scoreValue  = 1;

}

The only thing that should affect enemy health is the bullet. Here is the part of the bullet controller that affects enemy health:

 public void OnTriggerEnter(Collider other)
{
    if(other.tag == "Enemy")
    {
        triggeringEnemy = other.gameObject;
        triggeringEnemy.GetComponent<EnemyController>().health -= damage;
     
        PlayerController.instance.bulletPool.ReturnObject(gameObject);
    }

    
}

The issue always happens after 20 enemies have been spawned/destroyed and 20 points have been achieved. Before then, enemy movement is normal and everything seems to be working as it should. It could very well be something very simple that I'm missing, but I'd appreciate any help!

CodePudding user response:

Hello i think problem is here.

In your IEnumerator SpawnObject you increase your GameController.EnemyCount ; But when enemy Die() you dont decrease GameController.EnemyCount so when it reach your spawnSettings[0].maxObjects then it just stop spawn another enemy.

You should add GameController.EnemyCount--; inside enemy Die().

CodePudding user response:

When you do engage logic mistake good way to find it is Visual Studio debugging. Here is good official article about whole process - https://docs.unity3d.com/Manual/ManagedCodeDebugging.html.

I would place breakpoint right at start of Update method to see that EnemyCount increases over time and then you will understand that you forgot to do decrement it.

  • Related