Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by NightLucididty · Dec 11, 2015 at 08:43 PM · c#listmanual

ArgumentOutOfRangeException: Argument is out of range.

Dear forum, I know this may seem like a question that is asked all the time, but mine is a little different, Normally this error occurs when there is a for loop, but I couldn't use a for loop because of the requirements I needed. Therefore I created my own for loop. Ive tried over and over again to find a way to fix the error but no luck... Can anyone show me either what the error is or what the best way of turning my manual for loop into a normal for loop

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GhostPlayer : MonoBehaviour {
     
     public List<Vector2> playerMove;
     public List<float> timedMove;
     public List<float> animX;
     public List<float> animY;
     public List<bool> isGrounded;
     public List<bool> wallTouch;
 
     public GameObject MySprite;
     
     Vector3 MySpriteScale;
 
     MovementScript original;
 
     float startTime;
     float journeyLength;
 
     Animator animations;
     
     public int numberOfMoves = 0;
 
     void Start () { 
         var go = GameObject.FindGameObjectWithTag ("Player"); 
         original = go.GetComponent<MovementScript> ();
 
         MySpriteScale = MySprite.transform.localScale;
 
         animations = MySprite.GetComponent<Animator> ();
 
         playerMove = new List<Vector2>(original.playerPos);
         timedMove = new List<float> (original.timedPos);
         animX = new List<float> (original.animX);
         animY = new List<float> (original.animY);
         isGrounded = new List<bool> (original.isGrounded);
         wallTouch = new List<bool> (original.wallTouch);
 
         startTime = timedMove [0];
 
         original.playerPos.Clear ();
         original.timedPos.Clear ();
         original.animX.Clear ();
         original.animY.Clear ();
         original.isGrounded.Clear ();
         original.wallTouch.Clear ();
     }
     
     void Update() {
         journeyLength = Vector3.Distance(transform.position, playerMove [numberOfMoves+1]);
         float distCovered = (timedMove [numberOfMoves+1] - startTime);
         float fracJourney = distCovered / journeyLength;
         transform.position = Vector3.Lerp(transform.position, playerMove [numberOfMoves+1], fracJourney);
         
         animations.SetBool ("Grounded", isGrounded [numberOfMoves + 1]);
         animations.SetBool ("Walled", wallTouch [numberOfMoves + 1]);
         animations.SetFloat ("HorizontalSpeed", animX[numberOfMoves+1]);
         animations.SetFloat ("VerticalSpeed", animY[numberOfMoves+1]);
         
         if (transform.position.x == playerMove [numberOfMoves+1].x && transform.position.y == playerMove [numberOfMoves+1].y) {
             numberOfMoves++;
         }
         
         if(transform.position.x < playerMove [numberOfMoves+1].x) {
             MySprite.transform.localScale = MySpriteScale;
         } else if(transform.position.x > playerMove [numberOfMoves+1].x) {
             MySprite.transform.localScale = new Vector3(-MySpriteScale.x,MySpriteScale.y,MySpriteScale.z);
         }
 
         if(original.deadNinja) {
             original.deadNinja = false;
         }
     }
 }
Comment
Add comment · Show 3
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 Jessespike · Dec 11, 2015 at 08:56 PM 0
Share

Script won't compile without $$anonymous$$ovementScript. You didn't include the full error description - What line is the error on? You also mentioned loops, but I don't see any. So hard to answer your question.

avatar image NightLucididty Jessespike · Dec 11, 2015 at 09:17 PM 0
Share

Like I said I simulated my own for loop because a normal one didn't do what I wanted,

          journeyLength = Vector3.Distance(transform.position, player$$anonymous$$ove [numberOf$$anonymous$$oves+1]);
          float distCovered = (timed$$anonymous$$ove [numberOf$$anonymous$$oves+1] - startTime);
          float fracJourney = distCovered / journeyLength;
          transform.position = Vector3.Lerp(transform.position, player$$anonymous$$ove [numberOf$$anonymous$$oves+1], fracJourney);
          
          animations.SetBool ("Grounded", isGrounded [numberOf$$anonymous$$oves + 1]);
          animations.SetBool ("Walled", wallTouch [numberOf$$anonymous$$oves + 1]);
          animations.SetFloat ("HorizontalSpeed", animX[numberOf$$anonymous$$oves+1]);
          animations.SetFloat ("VerticalSpeed", animY[numberOf$$anonymous$$oves+1]);
          
          if (transform.position.x == player$$anonymous$$ove [numberOf$$anonymous$$oves+1].x && transform.position.y == player$$anonymous$$ove [numberOf$$anonymous$$oves+1].y) {
              numberOf$$anonymous$$oves++;
          }
          
          if(transform.position.x < player$$anonymous$$ove [numberOf$$anonymous$$oves+1].x) {
              $$anonymous$$ySprite.transform.localScale = $$anonymous$$ySpriteScale;
          } else if(transform.position.x > player$$anonymous$$ove [numberOf$$anonymous$$oves+1].x) {
              $$anonymous$$ySprite.transform.localScale = new Vector3(-$$anonymous$$ySpriteScale.x,$$anonymous$$ySpriteScale.y,$$anonymous$$ySpriteScale.z);
          }

As for the $$anonymous$$ovementScript there really is no need for that as all it does is give in the values for the lists. I know the problem is that the numberOf$$anonymous$$oves is still going up even after the list has ended, I just cant seem to find a way to stop it...

Here is the full error:

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.Vector2].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) GhostPlayer.Update () (at Assets/Platformer/Scripts/GhostPlayer.cs:53)

avatar image LazyElephant · Dec 11, 2015 at 09:18 PM 1
Share

You use this line a lot player$$anonymous$$ove [numberOf$$anonymous$$oves+1]and you increment numberOf$$anonymous$$oves, but I don't see in the code where you are checking to be sure numberOf$$anonymous$$oves+1 isn't larger than the number of vectors in your list. Are you certain that numberOf$$anonymous$$oves+1 is never more than the length of your list?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jessespike · Dec 11, 2015 at 09:29 PM

Yea, numberOfMoves is being incremented and is being used to access playerMove, but it's not checking if the index is valid. Maybe replace the direct accesses to playerMove with a Get function. As a test, you can try:

  //playerMove [numberOfMoves+1].x
  GetPlayerMove(numberOfMoves+1).x  

     Vector2 GetPlayerMove(int index)
     {
         if (index < playerMove.Count)
         {
             return playerMove[index];
         }
         else
         {
              Debug.LogError("Error while trying to access playerMove \nGetPlayerMove index = " + index + "\nplayerMove.Count = " + playerMove.Count);
         }            
         return Vector2.zero;
     }

Comment
Add comment · Show 1 · 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 NightLucididty · Dec 12, 2015 at 05:05 PM 0
Share

Yup thats exactly what I was looking for managed to modify it to make it work more like a normal usage rather than a test purpose. I did try to do something similar to this but it wouldn't work. Thanks again Jessespike P.S: Yes I did know why the error was co$$anonymous$$g I just didn't know how to stop it without breaking my script. Thanks anyway P.P.S: Convert your reply to an answer so I can accept it ;)

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

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How Do I Create a List of Two Strings? C# 1 Answer

List keeps losing items. 0 Answers

Updating a List from a Custom Inspector 0 Answers

Firebase List Users 1 Answer

How to remove items in a list based on if the player object is not in a collider object that has the same assigned number variable as the list item 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