the code is working pretty well at first until the AI manage to catch the 1st mouse in the array it stop chasing and it give the error that said “the object have been destroy but you still try to access it” which is weird cuz if the other object in array get destroy it still work perfectly.
here is the code
public Transform mouses;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 currentPos = transform.position;
Transform tMin = null;
float minDist = Mathf.Infinity;
foreach (Transform mouse in mouses)
{
float dist = Vector3.Distance(mouse.position, currentPos);
if (dist < minDist)
{
tMin = mouse;
minDist = dist;
transform.LookAt(tMin);
transform.position += transform.forward * 3.5f * Time.deltaTime;
}
Debug.Log(tMin);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "mouse")
{
Destroy(other.gameObject);
}
}