- Home /
Problem with chasing ai
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);
}
}
You should remove your destroyed mouse from your 'mouses' array when destroying the gameobject. I would recommend using a List ins$$anonymous$$d of an array
It also seems to me that in your search for the closest mouse, you should first deter$$anonymous$$e wich one is the closest (in your foreach loop) and save it as a variable, then only (after your loop) move your transform. As is, for each mouse that is closer than the previous, you'll look at it and move towards it. You should only do that to the ONE mouse that is the closest, and you cant know that before you finished your loop.
Ok I will go studies about list and see if it work.Ty sir.
@Gr$$anonymous$$l You should promote your comment to an answer. Also, a simple condition
if (mouse == null)
Will return true if the object has been destroyed.
so where should I add that to the line?
transform.LookAt(t$$anonymous$$in);
transform.position += transform.forward * 3.5f * Time.deltaTime;
These two lines look weird to me. I guess you want to found the closest mouse, then move to it. If that the case, these line should be outside the foreach loop.
Answer by Fluffy_Kaeloky · Dec 16, 2016 at 02:50 PM
As @GrKl said, you should use a list instead of an array, and use the condition if (mouse == null) to remove destroyed objects.
I modified your code :
public List<Transform> mouses = new List<Transform>();
void Update()
{
Vector3 currentPos = transform.position;
Transform tMin = null;
float minDist = Mathf.Infinity;
List<Transform> destroyedMouses = new List<Transform>();
foreach (Transform mouse in mouses)
{
if (mouse == null)
{
destroyedMouses.Add(mouse);
continue;
}
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);
}
foreach (Transform mouse in destroyedMouses)
mouses.Remove(mouse);
destroyedMouses.Clear();
}
I added destroyedMouses list since you can't edit mouses while in the loop, you would probably crash.
Humm... OP should really understand he should remove
transform.LookAt(t$$anonymous$$in);
transform.position += transform.forward * 3.5f * Time.deltaTime;
from his foreach loop. That loop should only search for the closest mouse and not do anything to them before the end of the loop.
Your answer
Follow this Question
Related Questions
How to make the NPC following players in multiplayer game 0 Answers
Help with a chasing script 2 Answers
Enemy chase script // No longer attacking 0 Answers
Can't make enemy respawn 0 Answers