I have a small issue here, that is driving me mad. I have read dozens of posts and tried each technique, but to no end.
The problem is, when my turret ability triggers the function, damage is done to all enemies like so:
So let me try to go through this step by step. BTW, I don’t have any errors at all in any of the code I will post, it just doesn’t function correctly.
Goal: Instantiate 10 enemies, each with their own unique stats. Enemy is destroyed when health reaches zero.
Code that spawns my enemies (InGameManager.cs):
IEnumerator wave01 ()
{
while (true)
{
for (int i = 0; i < totalEnemy; i++)
{
Vector3 spawnPosition = new Vector3 (InGameManagerPOS.x, InGameManagerPOS.y, InGameManagerPOS.z);
Instantiate (Frog, spawnPosition, Quaternion.identity);
yield return new WaitForSeconds (2);
}
enemyWave = EnemyWaves.wave02;
return false;
}
}
Code that controls the ability which can collide with enemies (TowerAbility.cs)(attached to my ability prefab):
void Start ()
{
//transform.position += temp;
my_current_enemy = GameObject.FindGameObjectWithTag ("Enemy");
}
// Update is called once per frame+
void Update ()
{
if (my_current_enemy)
{
transform.position = Vector3.Lerp (transform.position, my_current_enemy.transform.position, ability_velocity);
}
abilityDistance += Time.deltaTime * abilitySpeed;
if (abilityDistance >= abilityRange)
{
Destroy (gameObject);
}
if (my_current_enemy == null)
{
Destroy (gameObject);
}
}
}
Code that controls the damage done by the ability (WaveAI.cs)(attached to the enemy prefab):
public int enemyHealth = 10;
public int damage = 5;
void OnTriggerEnter (Collider theObject)
{
if(theObject.gameObject.tag=="tower_ability")
{
print (enemyHealth);
enemyHealth -= damage;
print (enemyHealth);
if (enemyHealth <= 0)
{
Destroy(gameObject);
}
else return;
}
}
Now, just to give some background I have tried SEVERAL methods, I am clearly missing something because they all seem to work the same way. Damage is done to all enemies.
I would like to get to the bottom of this so I can actually have a challenge in my game, instead of all 100 waves dying in 1 shot a piece, lol.
Thanks!!!