- Home /
Question by
majszi · Dec 08, 2020 at 09:45 PM ·
indexoutofrangeexception
Index out of range while random
public class AgentBehaviour : MonoBehaviour
{
private NavMeshAgent _navMeshAgent;
public Spawn agent;
public GameObject[] goals;
void Start()
{
agent = GetComponent<Spawn>();
_navMeshAgent = GetComponent<NavMeshAgent>();
SetGoal();
}
// Update is called once per frame
void Update()
{
}
void SetGoal()
{
_navMeshAgent.SetDestination(goals[Random.Range(0, 5)].transform.position);
}
}
I'm trying to assign random positions(from list of 5 objects) for my agents to go there. Each value(typed or drawn) results in "IndexOutOfRangeException:Index was outside the bounds of the array" in line _navMeshAgent.SetDestination(goals[Random.Range(0, 5)].transform.position); . Anyone has any ideas?
Comment
Answer by sacredgeometry · Dec 08, 2020 at 10:02 PM
You havent populated goals and if you have you have given it less than 6 values