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;
}
}
}
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.
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)
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?
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;
}
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
Follow this Question
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