- Home /
Question by
DwarvenGaming · Jul 28, 2015 at 09:01 AM ·
collideraicrashnavmeshagentoverlapsphere
Overlapsphere crashing my game?
My Unity keeps freezing when I run it while using this code, Am i doing something wrong?
using UnityEngine;
using System.Collections;
public class BasicAIMovement : MonoBehaviour {
NavMeshAgent nma;
public GameObject point;
// Use this for initialization
void Start () {
nma = gameObject.GetComponent<NavMeshAgent>();
Search(gameObject.transform.position,20);
}
// Update is called once per frame
void Update () {
if(point != null){
nma.destination = point.transform.position;
}
}
void Search(Vector3 center,float radius){
Collider[] hitColliders = Physics.OverlapSphere(center, radius);
int i = 0;
while (i < hitColliders.Length) {
if(hitColliders[i].tag == "Point"){
print(hitColliders[i].name);
i++;
}
}
}
}
Comment
Answer by YoungDeveloper · Jul 28, 2015 at 09:03 AM
You have an infinite loop, you are adding i only if tag is point, but it should actually be added regardless of any conditions.
while (i < hitColliders.Length) {
if(hitColliders[i].tag == "Point"){
print(hitColliders[i].name);
i++;
}
}
should be
while (i < hitColliders.Length) {
if(hitColliders[i].tag == "Point"){
print(hitColliders[i].name);
}
i++;
}