using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Walk : StateMachineBehaviour
{
float timer;
List Points = new List();
NavMeshAgent agent;
Transform Player;
float ChaseRange = 8;
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
Player = GameObject.FindGameObjectWithTag(“Player”).transform;
agent = animator.GetComponent<NavMeshAgent>();
agent.speed = 1.5f;
timer = 0;
Transform PointsObject = GameObject.FindGameObjectWithTag("Points").transform;
foreach (Transform t in PointsObject)
Points.Add(t);
agent.SetDestination(Points[0].position);
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (agent.remainingDistance <= agent.stoppingDistance)
agent.SetDestination(Points[Random.Range(0, Points.Count)].position);
timer += Time.deltaTime;
if (timer > 10)
animator.SetBool("IsPatrolling", false);
float distance = Vector3.Distance(Player.position, animator.transform.position);
if (distance < ChaseRange)
animator.SetBool("IsChasing", true);
}
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
agent.SetDestination(agent.transform.position);
}
}
this is the code and the error shows
agent.SetDestination(Points[Random.Range(0, Points.Count)].position);
agent.SetDestination(Points[Random.Range(0, Points.Count)].position);
on these two lines How to solve it ??
1 Like
Use this instead:
agent.SetDestination(Points[Random.Range(0, Points.Count - 1)].position);
In addition, you can check if a list element is not null before requesting a position.
I tried the above what you said but still it doesn’t work
Exact same as the above
Argument out of range exception: Index was out of range. Must be non-negative and less than the size of the collection
There is no a way to declare it like that: List Points = new List();
Paste your code properly in the original post.
You have initialized the List Points but the list is empty, you haven’t added anything in it. If you add some elements to the point list, the error will go away.
also, he is correct on that, because you haven’t formatted your code correctly, the <T>
has been lost, so we don’t know the type, edit your post to include all your code inside the code block.
To include code inside code blocks you add it between three backticks and the cs after the first three backticks:
```cs
# Your code here
```
for example the following:
```cs
List<Transform> WayPoints = new List<Transform>();
```
will show as:
List<Transform> WayPoints = new List<Transform>();
In any case, as I said the error happens because your List is empty.
I am new to programming how to solve it ??
Adding elements to a List, is pretty basic programming and happens with the Add method, but you need to know what you want to add, in your case what transforms to add as your list is a List<Transform>
. No one can answer that for you, you should know what transforms your code needs depending on what you are making.
My advice, if you don’t know how to add things to a list, is to stop what you are doing as I see it involves inheriting from a state machine, NavMeshAgent, and accessing random elements in lists, as these are more advanced concepts than your current level and find some beginner C# tutorials to learn the basics.
After that, you can start adding more advanced concepts one by one.
ArgumentOutOfRange error due to empty list of point try this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Walk : StateMachineBehaviour
{
float timer;
List<Transform> Points = new List<Transform>();
NavMeshAgent agent;
Transform Player;
float ChaseRange = 8;
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
Player = GameObject.FindGameObjectWithTag("Player").transform;
agent = animator.GetComponent<NavMeshAgent>();
agent.speed = 1.5f;
timer = 0;
Transform PointsObject = GameObject.FindGameObjectWithTag("Points").transform;
if (PointsObject != null)
{
foreach (Transform t in PointsObject)
{
Points.Add(t);
}
if (Points.Count > 0)
{
agent.SetDestination(Points[0].position);
}
}
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (Points.Count > 0 && agent.remainingDistance <= agent.stoppingDistance)
{
agent.SetDestination(Points[Random.Range(0, Points.Count)].position);
}
timer += Time.deltaTime;
if (timer > 10)
animator.SetBool("IsPatrolling", false);
float distance = Vector3.Distance(Player.position, animator.transform.position);
if (distance < ChaseRange)
animator.SetBool("IsChasing", true);
}
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
agent.SetDestination(agent.transform.position);
}
}