ArgumentOutOfRangeException: Argument is out of range. Parameter name:Index C# UNITY












-2















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.










share|improve this question

























  • 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 '18 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 '18 at 4:14











  • The else statement is where the error is generated. The end of the else if strand.

    – Mr Cuddles
    Nov 20 '18 at 4:15











  • WAIT if an array has 8 elements would count be 8 or 7.

    – Mr Cuddles
    Nov 20 '18 at 4:17











  • If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8

    – TheGeneral
    Nov 20 '18 at 4:19


















-2















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.










share|improve this question

























  • 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 '18 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 '18 at 4:14











  • The else statement is where the error is generated. The end of the else if strand.

    – Mr Cuddles
    Nov 20 '18 at 4:15











  • WAIT if an array has 8 elements would count be 8 or 7.

    – Mr Cuddles
    Nov 20 '18 at 4:17











  • If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8

    – TheGeneral
    Nov 20 '18 at 4:19
















-2












-2








-2








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 4:44







Mr Cuddles

















asked Nov 20 '18 at 3:51









Mr CuddlesMr 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 '18 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 '18 at 4:14











  • The else statement is where the error is generated. The end of the else if strand.

    – Mr Cuddles
    Nov 20 '18 at 4:15











  • WAIT if an array has 8 elements would count be 8 or 7.

    – Mr Cuddles
    Nov 20 '18 at 4:17











  • If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8

    – TheGeneral
    Nov 20 '18 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 '18 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 '18 at 4:14











  • The else statement is where the error is generated. The end of the else if strand.

    – Mr Cuddles
    Nov 20 '18 at 4:15











  • WAIT if an array has 8 elements would count be 8 or 7.

    – Mr Cuddles
    Nov 20 '18 at 4:17











  • If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8

    – TheGeneral
    Nov 20 '18 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 '18 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 '18 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 '18 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 '18 at 4:14













The else statement is where the error is generated. The end of the else if strand.

– Mr Cuddles
Nov 20 '18 at 4:15





The else statement is where the error is generated. The end of the else if strand.

– Mr Cuddles
Nov 20 '18 at 4:15













WAIT if an array has 8 elements would count be 8 or 7.

– Mr Cuddles
Nov 20 '18 at 4:17





WAIT if an array has 8 elements would count be 8 or 7.

– Mr Cuddles
Nov 20 '18 at 4:17













If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8

– TheGeneral
Nov 20 '18 at 4:19







If an array has 8 elements, the indexes will be 0 -- 7, the count will be 8

– TheGeneral
Nov 20 '18 at 4:19














1 Answer
1






active

oldest

votes


















0














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






share|improve this answer



















  • 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 '18 at 5:04











  • yes, that's what happened

    – Mr Cuddles
    Nov 20 '18 at 21:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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






share|improve this answer



















  • 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 '18 at 5:04











  • yes, that's what happened

    – Mr Cuddles
    Nov 20 '18 at 21:25
















0














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






share|improve this answer



















  • 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 '18 at 5:04











  • yes, that's what happened

    – Mr Cuddles
    Nov 20 '18 at 21:25














0












0








0







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 4:53









Mr CuddlesMr Cuddles

11




11








  • 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 '18 at 5:04











  • yes, that's what happened

    – Mr Cuddles
    Nov 20 '18 at 21:25














  • 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 '18 at 5:04











  • yes, that's what happened

    – Mr Cuddles
    Nov 20 '18 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 '18 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 '18 at 5:04













yes, that's what happened

– Mr Cuddles
Nov 20 '18 at 21:25





yes, that's what happened

– Mr Cuddles
Nov 20 '18 at 21:25


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

WPF add header to Image with URL pettitions [duplicate]