ArgumentOutOfRangeException: Argument is out of range. Parameter name:Index C# UNITY
up vote
-2
down vote
favorite
if (Input.GetAxis("Horizontal") > 0)
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.RIGHT;
else if (Input.GetAxis("Horizontal") < 0)
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.LEFT;
else if (Input.GetAxis("Vertical") > 0)
{
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.UP;
}
else if (Input.GetAxis("Vertical") < 0)
{
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.DOWN;
}
else{
board[bodies[bodies.Count-1].x, bodies[bodies.Count - 1].y].direction = board[bodies[bodies.Count - 2].x, bodies[bodies.Count - 2].x].direction;
}
<-code generating error.
Init(start statement)::
bodySize = bodyOriginal.transform.localScale.x;
TopRight = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
BottomLeft = Camera.main.ScreenToWorldPoint(new Vector2(0, 0));
TopRight += new Vector2(bodySize, bodySize);
BottomLeft += new Vector2(bodySize, bodySize);
BOARD_HEIGHT = (int)(TopRight.y / bodySize);
BOARD_WIDTH = (int)(TopRight.x / bodySize);
board = new GameManager.BoardState[BOARD_WIDTH, BOARD_HEIGHT];
board[1, 1].isSnake = Objects.SNAKE;
board[1, 2].isSnake = Objects.SNAKE;
board[1, 1].direction = Direction.RIGHT;
board[1, 2].direction = Direction.RIGHT;
Body head = Instantiate(bodyOriginal) as Body;
Body tail = Instantiate(bodyOriginal) as Body;
tail.GetComponent<SpriteRenderer>().sprite = tailSprite;
head.GetComponent<SpriteRenderer>().sprite = headSprite;
head.x = 1;
head.y = 0;
tail.x = 0;
head.y = 0;
tail.transform.localPosition = new Vector2(BottomLeft.x + (tail.x * bodySize), BottomLeft.y + (tail.y * bodySize));
head.transform.localPosition = new Vector2(BottomLeft.x + (head.x * bodySize), BottomLeft.y + (head.y * bodySize));
bodies.Add(head);
bodies.Add(tail);
food.SpawnRandom();
I think the problem is with the Init. Given that in the else statement bodies.Count - 1 is -1.
Apparently, this post has to have a certain text to code ratio in which I do not currently have met in this current sequential moment of time, I hope that this policy will be changed soon.
c# unity3d
|
show 7 more comments
up vote
-2
down vote
favorite
if (Input.GetAxis("Horizontal") > 0)
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.RIGHT;
else if (Input.GetAxis("Horizontal") < 0)
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.LEFT;
else if (Input.GetAxis("Vertical") > 0)
{
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.UP;
}
else if (Input.GetAxis("Vertical") < 0)
{
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.DOWN;
}
else{
board[bodies[bodies.Count-1].x, bodies[bodies.Count - 1].y].direction = board[bodies[bodies.Count - 2].x, bodies[bodies.Count - 2].x].direction;
}
<-code generating error.
Init(start statement)::
bodySize = bodyOriginal.transform.localScale.x;
TopRight = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
BottomLeft = Camera.main.ScreenToWorldPoint(new Vector2(0, 0));
TopRight += new Vector2(bodySize, bodySize);
BottomLeft += new Vector2(bodySize, bodySize);
BOARD_HEIGHT = (int)(TopRight.y / bodySize);
BOARD_WIDTH = (int)(TopRight.x / bodySize);
board = new GameManager.BoardState[BOARD_WIDTH, BOARD_HEIGHT];
board[1, 1].isSnake = Objects.SNAKE;
board[1, 2].isSnake = Objects.SNAKE;
board[1, 1].direction = Direction.RIGHT;
board[1, 2].direction = Direction.RIGHT;
Body head = Instantiate(bodyOriginal) as Body;
Body tail = Instantiate(bodyOriginal) as Body;
tail.GetComponent<SpriteRenderer>().sprite = tailSprite;
head.GetComponent<SpriteRenderer>().sprite = headSprite;
head.x = 1;
head.y = 0;
tail.x = 0;
head.y = 0;
tail.transform.localPosition = new Vector2(BottomLeft.x + (tail.x * bodySize), BottomLeft.y + (tail.y * bodySize));
head.transform.localPosition = new Vector2(BottomLeft.x + (head.x * bodySize), BottomLeft.y + (head.y * bodySize));
bodies.Add(head);
bodies.Add(tail);
food.SpawnRandom();
I think the problem is with the Init. Given that in the else statement bodies.Count - 1 is -1.
Apparently, this post has to have a certain text to code ratio in which I do not currently have met in this current sequential moment of time, I hope that this policy will be changed soon.
c# unity3d
Have you debugged this? and what has the results provided you with, what line does it look like its going wrong with the index, what is your expert opinion
– TheGeneral
Nov 20 at 3:53
ArgumentOutOfRangeException with Index parameter is mostly occurred when index is literally "Out of range". such as accessing less than 0 (ARRAY started from 0, so Index cannot goes below 0) OR bigger/equal thanArray.Length
– SlaneR
Nov 20 at 4:14
The else statement is where the error is generated. The end of the else if strand.
– Mr Cuddles
Nov 20 at 4:15
WAIT if an array has 8 elements would count be 8 or 7.
– Mr Cuddles
Nov 20 at 4:17
If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8
– TheGeneral
Nov 20 at 4:19
|
show 7 more comments
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
if (Input.GetAxis("Horizontal") > 0)
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.RIGHT;
else if (Input.GetAxis("Horizontal") < 0)
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.LEFT;
else if (Input.GetAxis("Vertical") > 0)
{
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.UP;
}
else if (Input.GetAxis("Vertical") < 0)
{
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.DOWN;
}
else{
board[bodies[bodies.Count-1].x, bodies[bodies.Count - 1].y].direction = board[bodies[bodies.Count - 2].x, bodies[bodies.Count - 2].x].direction;
}
<-code generating error.
Init(start statement)::
bodySize = bodyOriginal.transform.localScale.x;
TopRight = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
BottomLeft = Camera.main.ScreenToWorldPoint(new Vector2(0, 0));
TopRight += new Vector2(bodySize, bodySize);
BottomLeft += new Vector2(bodySize, bodySize);
BOARD_HEIGHT = (int)(TopRight.y / bodySize);
BOARD_WIDTH = (int)(TopRight.x / bodySize);
board = new GameManager.BoardState[BOARD_WIDTH, BOARD_HEIGHT];
board[1, 1].isSnake = Objects.SNAKE;
board[1, 2].isSnake = Objects.SNAKE;
board[1, 1].direction = Direction.RIGHT;
board[1, 2].direction = Direction.RIGHT;
Body head = Instantiate(bodyOriginal) as Body;
Body tail = Instantiate(bodyOriginal) as Body;
tail.GetComponent<SpriteRenderer>().sprite = tailSprite;
head.GetComponent<SpriteRenderer>().sprite = headSprite;
head.x = 1;
head.y = 0;
tail.x = 0;
head.y = 0;
tail.transform.localPosition = new Vector2(BottomLeft.x + (tail.x * bodySize), BottomLeft.y + (tail.y * bodySize));
head.transform.localPosition = new Vector2(BottomLeft.x + (head.x * bodySize), BottomLeft.y + (head.y * bodySize));
bodies.Add(head);
bodies.Add(tail);
food.SpawnRandom();
I think the problem is with the Init. Given that in the else statement bodies.Count - 1 is -1.
Apparently, this post has to have a certain text to code ratio in which I do not currently have met in this current sequential moment of time, I hope that this policy will be changed soon.
c# unity3d
if (Input.GetAxis("Horizontal") > 0)
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.RIGHT;
else if (Input.GetAxis("Horizontal") < 0)
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.LEFT;
else if (Input.GetAxis("Vertical") > 0)
{
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.UP;
}
else if (Input.GetAxis("Vertical") < 0)
{
board[bodies[bodies.Count - 1].x, bodies[bodies.Count - 1].y].direction = Direction.DOWN;
}
else{
board[bodies[bodies.Count-1].x, bodies[bodies.Count - 1].y].direction = board[bodies[bodies.Count - 2].x, bodies[bodies.Count - 2].x].direction;
}
<-code generating error.
Init(start statement)::
bodySize = bodyOriginal.transform.localScale.x;
TopRight = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
BottomLeft = Camera.main.ScreenToWorldPoint(new Vector2(0, 0));
TopRight += new Vector2(bodySize, bodySize);
BottomLeft += new Vector2(bodySize, bodySize);
BOARD_HEIGHT = (int)(TopRight.y / bodySize);
BOARD_WIDTH = (int)(TopRight.x / bodySize);
board = new GameManager.BoardState[BOARD_WIDTH, BOARD_HEIGHT];
board[1, 1].isSnake = Objects.SNAKE;
board[1, 2].isSnake = Objects.SNAKE;
board[1, 1].direction = Direction.RIGHT;
board[1, 2].direction = Direction.RIGHT;
Body head = Instantiate(bodyOriginal) as Body;
Body tail = Instantiate(bodyOriginal) as Body;
tail.GetComponent<SpriteRenderer>().sprite = tailSprite;
head.GetComponent<SpriteRenderer>().sprite = headSprite;
head.x = 1;
head.y = 0;
tail.x = 0;
head.y = 0;
tail.transform.localPosition = new Vector2(BottomLeft.x + (tail.x * bodySize), BottomLeft.y + (tail.y * bodySize));
head.transform.localPosition = new Vector2(BottomLeft.x + (head.x * bodySize), BottomLeft.y + (head.y * bodySize));
bodies.Add(head);
bodies.Add(tail);
food.SpawnRandom();
I think the problem is with the Init. Given that in the else statement bodies.Count - 1 is -1.
Apparently, this post has to have a certain text to code ratio in which I do not currently have met in this current sequential moment of time, I hope that this policy will be changed soon.
c# unity3d
c# unity3d
edited Nov 20 at 4:44
asked Nov 20 at 3:51
Mr Cuddles
11
11
Have you debugged this? and what has the results provided you with, what line does it look like its going wrong with the index, what is your expert opinion
– TheGeneral
Nov 20 at 3:53
ArgumentOutOfRangeException with Index parameter is mostly occurred when index is literally "Out of range". such as accessing less than 0 (ARRAY started from 0, so Index cannot goes below 0) OR bigger/equal thanArray.Length
– SlaneR
Nov 20 at 4:14
The else statement is where the error is generated. The end of the else if strand.
– Mr Cuddles
Nov 20 at 4:15
WAIT if an array has 8 elements would count be 8 or 7.
– Mr Cuddles
Nov 20 at 4:17
If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8
– TheGeneral
Nov 20 at 4:19
|
show 7 more comments
Have you debugged this? and what has the results provided you with, what line does it look like its going wrong with the index, what is your expert opinion
– TheGeneral
Nov 20 at 3:53
ArgumentOutOfRangeException with Index parameter is mostly occurred when index is literally "Out of range". such as accessing less than 0 (ARRAY started from 0, so Index cannot goes below 0) OR bigger/equal thanArray.Length
– SlaneR
Nov 20 at 4:14
The else statement is where the error is generated. The end of the else if strand.
– Mr Cuddles
Nov 20 at 4:15
WAIT if an array has 8 elements would count be 8 or 7.
– Mr Cuddles
Nov 20 at 4:17
If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8
– TheGeneral
Nov 20 at 4:19
Have you debugged this? and what has the results provided you with, what line does it look like its going wrong with the index, what is your expert opinion
– TheGeneral
Nov 20 at 3:53
Have you debugged this? and what has the results provided you with, what line does it look like its going wrong with the index, what is your expert opinion
– TheGeneral
Nov 20 at 3:53
ArgumentOutOfRangeException with Index parameter is mostly occurred when index is literally "Out of range". such as accessing less than 0 (ARRAY started from 0, so Index cannot goes below 0) OR bigger/equal than
Array.Length– SlaneR
Nov 20 at 4:14
ArgumentOutOfRangeException with Index parameter is mostly occurred when index is literally "Out of range". such as accessing less than 0 (ARRAY started from 0, so Index cannot goes below 0) OR bigger/equal than
Array.Length– SlaneR
Nov 20 at 4:14
The else statement is where the error is generated. The end of the else if strand.
– Mr Cuddles
Nov 20 at 4:15
The else statement is where the error is generated. The end of the else if strand.
– Mr Cuddles
Nov 20 at 4:15
WAIT if an array has 8 elements would count be 8 or 7.
– Mr Cuddles
Nov 20 at 4:17
WAIT if an array has 8 elements would count be 8 or 7.
– Mr Cuddles
Nov 20 at 4:17
If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8
– TheGeneral
Nov 20 at 4:19
If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8
– TheGeneral
Nov 20 at 4:19
|
show 7 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
I divided the world point value of the camera by the local scale of the body object giving my array ridiculously low values like 2 and 3, which lead me to go out of index in my start statement which made it skip right to update with an uninitialized array.
Note: Make sure to divide worldpoint by worldpoint in unity
1
Meaning you didn't provide the complete information and already got an exception before in theStartyou didn't tell us about?
– derHugo
Nov 20 at 5:04
yes, that's what happened
– Mr Cuddles
Nov 20 at 21:25
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I divided the world point value of the camera by the local scale of the body object giving my array ridiculously low values like 2 and 3, which lead me to go out of index in my start statement which made it skip right to update with an uninitialized array.
Note: Make sure to divide worldpoint by worldpoint in unity
1
Meaning you didn't provide the complete information and already got an exception before in theStartyou didn't tell us about?
– derHugo
Nov 20 at 5:04
yes, that's what happened
– Mr Cuddles
Nov 20 at 21:25
add a comment |
up vote
0
down vote
I divided the world point value of the camera by the local scale of the body object giving my array ridiculously low values like 2 and 3, which lead me to go out of index in my start statement which made it skip right to update with an uninitialized array.
Note: Make sure to divide worldpoint by worldpoint in unity
1
Meaning you didn't provide the complete information and already got an exception before in theStartyou didn't tell us about?
– derHugo
Nov 20 at 5:04
yes, that's what happened
– Mr Cuddles
Nov 20 at 21:25
add a comment |
up vote
0
down vote
up vote
0
down vote
I divided the world point value of the camera by the local scale of the body object giving my array ridiculously low values like 2 and 3, which lead me to go out of index in my start statement which made it skip right to update with an uninitialized array.
Note: Make sure to divide worldpoint by worldpoint in unity
I divided the world point value of the camera by the local scale of the body object giving my array ridiculously low values like 2 and 3, which lead me to go out of index in my start statement which made it skip right to update with an uninitialized array.
Note: Make sure to divide worldpoint by worldpoint in unity
answered Nov 20 at 4:53
Mr Cuddles
11
11
1
Meaning you didn't provide the complete information and already got an exception before in theStartyou didn't tell us about?
– derHugo
Nov 20 at 5:04
yes, that's what happened
– Mr Cuddles
Nov 20 at 21:25
add a comment |
1
Meaning you didn't provide the complete information and already got an exception before in theStartyou didn't tell us about?
– derHugo
Nov 20 at 5:04
yes, that's what happened
– Mr Cuddles
Nov 20 at 21:25
1
1
Meaning you didn't provide the complete information and already got an exception before in the
Start you didn't tell us about?– derHugo
Nov 20 at 5:04
Meaning you didn't provide the complete information and already got an exception before in the
Start you didn't tell us about?– derHugo
Nov 20 at 5:04
yes, that's what happened
– Mr Cuddles
Nov 20 at 21:25
yes, that's what happened
– Mr Cuddles
Nov 20 at 21:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385964%2fargumentoutofrangeexception-argument-is-out-of-range-parameter-nameindex-c-sh%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you debugged this? and what has the results provided you with, what line does it look like its going wrong with the index, what is your expert opinion
– TheGeneral
Nov 20 at 3:53
ArgumentOutOfRangeException with Index parameter is mostly occurred when index is literally "Out of range". such as accessing less than 0 (ARRAY started from 0, so Index cannot goes below 0) OR bigger/equal than
Array.Length– SlaneR
Nov 20 at 4:14
The else statement is where the error is generated. The end of the else if strand.
– Mr Cuddles
Nov 20 at 4:15
WAIT if an array has 8 elements would count be 8 or 7.
– Mr Cuddles
Nov 20 at 4:17
If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8
– TheGeneral
Nov 20 at 4:19