Unity C#ArgumentOutOfRangeException:参数超出范围。 参数名称:index

我正在创造一个像蛇一样的游戏。 在我下面的代码中,蛇体的每个片段都是Character类的一个实例。 当我尝试添加新角色时,我收到错误:

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index 

网上的其他资源已经提出有一个我想要引用的空列表。 但是在我的代码中我没有看到任何关于这种情况的证据。

也许比我更聪明的人可以找到我做错了什么?

 using System.Collections; using System.Collections.Generic; using UnityEngine; /* * This class controls the entire snake. The snake has a list * of segments that are each a character. We can move along the snake * by moving the characters bottom up, so that the character replaces * the position of the character before him. */ public class PlayerController : MonoBehaviour { //how many units will he move per 'frame' public float moveUnits = 1; //how often he will move (every quarter second) public float moveTimer = 0.25f; private float timeSinceLastMoved; public KeyCode dir = KeyCode.RightArrow; //locations of boundaries public float topBoundary = 4.5f; public float bottomBoundary = -4.5f; public float leftBoundary = -8.5f; public float rightBoundary = 8.5f; //Holds all characters for the snake body public List chars = new List(); // Use this for initialization void Start () { timeSinceLastMoved = Time.time; getFirstCharacter (); } // Update is called once per frame void Update () { getInput (); if (timeSinceLastMoved + moveTimer  0; i++) { chars [i].transform.position = chars [i - 1].transform.position; } } } void addCharacter() { //the position of the last segment Vector2 prevCharPos = chars[chars.Count-1].transform.position; Vector2 pos; float x = 0; float y = 0; switch (dir) { case KeyCode.RightArrow: x = prevCharPos.x - moveUnits; y = prevCharPos.y; break; case KeyCode.LeftArrow: x = prevCharPos.x + moveUnits; y = prevCharPos.y;; break; case KeyCode.UpArrow: x = prevCharPos.x; y = prevCharPos.y + moveUnits; break; case KeyCode.DownArrow: x = prevCharPos.x; y = prevCharPos.y - moveUnits; break; default: break; } pos = new Vector2 (x, y); //make a new character at the position behind the last segment Character newChar = Instantiate (chars[chars.Count - 1], pos, Quaternion.identity, this.transform); //add him to the list chars.Add (newChar); } void getFirstCharacter() { //find the character that already exists and add him to the list GameObject firstChar = GameObject.Find ("Character"); if (firstChar != null) { chars.Add(firstChar.GetComponent()); } } 

}

更改FOR循环条件。 从理论上讲,它将超出范围exception。

for (int i = chars.Count - 1; i > 0; i++) chars [i].transform.position = chars [i - 1].transform.position;

我相信你的意图应该是

for (int i = chars.Count - 1; i > 0; i--)

只需将你的循环放入if检查中

  if(chars.Count>0){ //for all characters(aka snake segments) //take the position of the segment before you for (int i = chars.Count - 1; i > 0; i++) { chars [i].transform.position = chars [i - 1].transform.position; } } else{ Debug.Log("Warning:: chars Count is less than 1"); } 

根据MSDN文档, ArgumentOutOfRangeException是:

当参数的值超出被调用方法定义的允许值范围时引发的exception

这意味着,当您尝试根据其索引值访问集合中的项目但实际上该索引不在索引范围内时,将引发exception。 大多数情况下集合遵循基于0的索引,因此如果您的特定值低于0或者它大于集合中元素的数量,则会在访问时导致此exception。

当我们浏览给定的片段时,我们可以看到你在不同的地方使用了chars [i - 1] ,显然当i值为0时会导致exception。

简而言之,我们可以说,让chars成为集合,并且您正在根据索引访问项目,如果您使用i-1访问前一项目,则检查chars.Count > 0非常重要,并且循环限制应该是少于chars.Count

这可能是一个通用的答案,但希望它可以帮助您理解此错误的原因以及如何解决它们