Scripting issue (not updating static int enemiesAlive)

Hi guys and girls

Currently I’m trying to make a 3d tower defense game but now I’ve run into problems…

I’ve got these scripts, but when one of my turret kills an enemy it doesnt update how many enemies there’s alive in the scene. Looks like it doesnt execute my Die() function in Enemy.CS

Here’s my code:

Enemy.cs:

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

public class Enemy : MonoBehaviour {
    public float startSpeed = 10f;
 
    [HideInInspector]
    public float speed;

    public float health = 100;

  
    private PlayerStats thePlayerStats;
    public int expToGive;

    public int worth = 50;

    public GameObject deathEffect;

    private bool isDead = false;


    void Start()
    {
        speed = startSpeed;
        thePlayerStats = FindObjectOfType<PlayerStats>();
    
    }

    public void TakeDamage(float amount)
    {
    
            health -= amount;

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

    public void Slow(float pct)
    {
        speed = startSpeed * (1f - pct);

    }

    void Die()
    {
    
        thePlayerStats.AddExperience(expToGive);
        isDead = true;

        PlayerStats.Money += worth;

        GameObject effect = (GameObject)Instantiate(deathEffect, transform.position, Quaternion.identity);
        Destroy(effect, 5f);

    
        WaveSpawner.EnemiesAlive--;
    
        Destroy(gameObject);

    }
}

Here’s my WaveSpawner.CS file:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class WaveSpawner : MonoBehaviour
{

    public static int EnemiesAlive = 0;

    public Wave[] waves;


    public Transform spawnPoint;
    public float timeBetweenWaves = 5f;
    private float countdown = 2f;

    public Text waveCountdownText;
    public Text enemiesAliveText;
    private int waveIndex = 0;





    void OnEnable()
    {
        EnemiesAlive = 0;
    }

    void Update()
    {



        enemiesAliveText.text = "Enemies alive:" + EnemiesAlive;

        if (EnemiesAlive > 0)
        {
            return;
        }

        if (waveIndex == waves.Length)
        {
            Debug.Log("LEVEL WON!");
            this.enabled = false;
        }

        if (countdown <= 0f)
        {
            StartCoroutine(SpawnWave());
            countdown = timeBetweenWaves;
            return;
        }

        countdown -= Time.deltaTime;

        countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);

        waveCountdownText.text = string.Format("{0:00:00}", countdown);


  
    }
    IEnumerator SpawnWave()
    {
        PlayerStats.Rounds++;

        Wave wave = waves[waveIndex];



        for (int i = 0; i < wave.count; i++)
        {
            SpawnEnemy(wave.enemy);
            yield return new WaitForSeconds(1f / wave.rate);
        }

        for (int i = 0; i < wave.count; i++)

        {




            waveIndex++;
        }
    }

    void SpawnEnemy(GameObject enemy)
    {
        Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
        EnemiesAlive++;

    }

}

And finally Wave.cs:

using UnityEngine;

[System.Serializable]
public class Wave{

    public GameObject enemy;

    public int count;

    public float rate;



}

Thank you in advance!

A couple of things here…
1: It’s only necessary to post relevant parts of code when you’re talking about something. That was really long :slight_smile:
2: It kinda looks okay to me. You should try some ‘Debug.Log()’ calls to check the values at certain spots for the count to make sure it’s going okay.
3: this is important → please read this: Using code tags properly - Unity Engine - Unity Discussions so that you can insert code into the forums in a more friendly manner (easier to read, etc).

Please write back with an update with regard to the logs and what values they show (when/where/etc). :slight_smile:

Just changed your third suggestion. Thank you for your quick reply

I’'ve been trying to do Debug.Log at the top of my Die() function but it does not execute the line.

That’s a bit odd. The enemy takes damage and dies, but you can’t print out a statement in the method?
It’s nice that you changed the code, it looks much better.

It should be printing something out, based on what you’ve said & shown…

After doing some debugging I noticed that if I kill my enemy with a certain turret (laser turret) the code is working as intended but with the other turrets my enemy gets 1shotted and gets destroyed without doing the line of code…

guessing the problem is within my code somewhere else .

Well, Die() should still be called, even if one-shotted… unless you have some code that kills it off before getting that far.
I’m glad you’re narrowing it down - sorry I can’t give more specific feedback. It’s a lot to sift through in your post.

If you narrow it down further, and have any follow up… feel free to add it here :slight_smile: Hope you figure it out.

yea it should be called but I wonder how it gets oneshotted when the enemyhealth is insanely high and the turrets damage is really low.

Thank you for your fast replies. Encourages me to continue to troubleshoot this problem until it has been solved.

Okay, no problem. Glad to help. Hope you get it solved. Does sound like a small mystery on your hands :slight_smile:
heh.

So I finally fixed it and I believe it’s because I changed the length of my array but I’m not sure

Glad you fixed it.