- Home /
noob needs c# help with generic lists
first time trying to use lists in C# I can add my objects in the inspector and my vehicle follows the waypoints but i keep getting the
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
any help would be appreciated.
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class WaypointSystem : MonoBehaviour {
public List<Transform>wayPoint = new List<Transform>() ;
public Transform currWayPoint;
private Transform myTransform;
public float moveSpeed=1;
public float rotationSpeed=1;
private int i = 0;
// Use this for initialization
void Start () {
myTransform = transform;
}
// Update is called once per frame
void Update ()
{
float distance = Vector3.Distance(wayPoint[i].position, transform.position);
Debug.DrawLine(wayPoint[i].transform.position, myTransform.position);
//Look at target_one
myTransform.rotation =Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation
(wayPoint[i].transform.position - myTransform.position),rotationSpeed *Time.deltaTime);
//Move Towards target_one
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
if (distance<10)
{
i++;
Debug.Log(i);
}
}
}
Answer by syclamoth · Jun 04, 2012 at 04:16 AM
If you keep incrementing 'i', it will inevitably run out of things in the list. You need to include some check to make sure that 'i' is always below 'wayPoint.Count'.
thanks for your reply. I added an if statement
if (i<=wayPoint.Count-1) and it worked. I tried it first without the -1 and still got the error but when I added the -1 it got rid of it. If you don't $$anonymous$$d could you explain why I couldnt just use the .count?
@adam420:
A List, exactly as an array contains a certain amount of entities. .Length (array) and .Count(List) returns the entity-count, lets say 4.
The index number you use to access a certain element is 0-based. That means the first element has the index 0 the second element the index 1 and so on. That means the highest possible index is always one element smaller than the count.
For our 4 element example the indices are: 0, 1, 2, 3
So the highest possible index is 3 which is the count (4) $$anonymous$$us 1
btw. the noob needs also some training in using a Q&A site ;) Don't post an Answer if you're not going to answer the question. Use comments ins$$anonymous$$d.
I've converted $$anonymous$$leren's comment into an answer and your answer into a comment ;)
edit
Just to simplify things a bit, those two lines are exactly the same:
if ( i <= wayPoint.Count -1 )
if ( i < wayPoint.Count )
That's why you see most for loops look like this
for(int i = 0; i < count; i++)
they run as long as i is smaller than the count. The biggest number that is smaller than count is count-1 ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613072325im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Set size of generic list via script? 0 Answers
Save public List contents of monobehaviour object edited through custom editor tool 1 Answer
Copy one Generic List to another one 2 Answers
Emptying a Generic List / Unexpected Behaviour 1 Answer
How to convert builtin arrays to generic lists in boo? 1 Answer