I have this code here and basically I have a list of enemies that are in the range and I hit the first one. For some reason when I place multiple towers (im making a tower defense game btw) they just stop doing damage to the enemies at all.
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class RangeDetectionAndDamage : MonoBehaviour
{
public EnemyStats enemyStats;
public TowerHover towerHover;
TowerStats towerStats;
bool canHit = true;
bool hasCoroutineStarted;
public List<GameObject> enemiesInRange = new();
Transform towerMesh;
Transform rotRoot;
public float damage;
public float attackCooldown;
public float moneyGained;
// Start is called before the first frame update
void Start()
{
towerHover = FindObjectOfType<TowerHover>();
towerMesh = transform.parent;
rotRoot = transform.parent.parent;
towerStats = GetComponentInParent<TowerStats>();
transform.localScale = towerStats.rangeSize;
}
// Update is called once per frame
void Update()
{
if (this.gameObject == null || enemyStats == null)
return;
print(rotRoot);
enemyStats.UpdateHealthBar(enemyStats.maxHealth, enemyStats.currHealth);
Vector3 targetPosition = new Vector3(enemyStats.gameObject.transform.position.x, transform.position.y, enemyStats.gameObject.transform.position.z);
rotRoot.rotation = Quaternion.LookRotation(targetPosition - transform.position, transform.up);
if (enemiesInRange.Count == 0)
return;
if (enemiesInRange[0].GetComponent<EnemyStats>().isDead)
{
enemiesInRange.Remove(enemiesInRange[0]);
}
}
private void OnTriggerStay(Collider other)
{
if (other.CompareTag("Enemy") && canHit && towerHover.wasPlaced && !hasCoroutineStarted)
{
if (enemiesInRange == null)
return;
enemyStats = enemiesInRange[0].GetComponent<EnemyStats>();
hasCoroutineStarted = true;
StartCoroutine(attackEnemy());
}
}
IEnumerator attackEnemy()
{
while (true)
{
if (enemiesInRange[0] == null)
yield break;
Debug.Log(canHit);
canHit = false;
enemiesInRange[0].GetComponent<EnemyStats>().currHealth -= towerStats.damage;
yield return new WaitForSeconds(towerStats.attackCooldown);
canHit = true;
yield return null;
hasCoroutineStarted = false;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
//hasCoroutineStarted = true;
enemiesInRange.Add(other.gameObject);
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Enemy"))
{
enemiesInRange.Remove(other.gameObject);
}
}
}