Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by adam420 · Jun 04, 2012 at 04:14 AM · generic list

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);
     }
     
     }   

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3

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'.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adam420 · Jun 04, 2012 at 06:59 PM 0
Share

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?

avatar image Bunny83 · Jun 04, 2012 at 07:05 PM 2
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges