I have 2 game objects a player and an enemy. The player has a rigidbody and an AttackRange named collider set as trigger assigned to it as a child object and it has a script that detects enemies in range then puts them in a List, if you move out of range it removes it. The enemy has a collider as well set as a trigger. Now my problem is that even though the enemy is in the AttackRange it randomly removes it from the lost for no apparent reason, I’ve already wasted 2 afternoons on this looking for answers to my problem, but to no avail.
Here is the AttackRange script.
using UnityEngine;
using System.Collections.Generic;
public class AttackRange : MonoBehaviour {
public List<GameObject> enemiesInRange = new List<GameObject>();
float timer;
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
}
public void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Enemy" && other.isTrigger == true) {
if(enemiesInRange.Contains(other.gameObject)){
} else {
enemiesInRange.Add(other.gameObject);
print ("Added: " + other.gameObject + " at " + timer);
}
}
}
public void OnTriggerExit(Collider other){
if (other.gameObject.tag == "Enemy" && other.isTrigger == true) {
if(enemiesInRange.Contains(other.gameObject)){
enemiesInRange.Remove(other.gameObject);
print ("Removed: " + other.gameObject + " at " + timer);
timer = 0.0f;
} else {
}
}
}
public List<GameObject> getEnemiesInRange(){
return enemiesInRange;
}
}