Unity C#ArgumentOutOfRangeException:参数超出范围.参数名称:索引 [英] Unity C# ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
问题描述
我正在创建一个像蛇一样的游戏.在下面的代码中,蛇的身体的每个部分都是Character类的一个实例.尝试添加新字符时,出现错误:
I'm creating a game that is like snake. In my code below, each segment of the snake's body is an instance of the Character class. When I try to add a new character, I get the error:
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
其他在线资源建议我要引用的列表为空.但是我在代码中看不到任何证据.
Other resources online have proposed that there's a List that is empty that I'm trying to reference. But I don't see any evidence of that case in my code.
也许一个比我聪明的人会发现我做错了什么?
Perhaps someone smarter than myself can find what I've done wrong?
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<Character> chars = new List<Character>();
// Use this for initialization
void Start () {
timeSinceLastMoved = Time.time;
getFirstCharacter ();
}
// Update is called once per frame
void Update () {
getInput ();
if (timeSinceLastMoved + moveTimer < Time.time) {
move ();
timeSinceLastMoved = Time.time;
}
}
void getInput() {
//if i press right go right, but i can't be going left when i want to go right
if (Input.GetKeyDown (KeyCode.RightArrow) && dir != KeyCode.LeftArrow) {
dir = KeyCode.RightArrow;
} else if (Input.GetKeyDown (KeyCode.LeftArrow) && dir != KeyCode.RightArrow) {
dir = KeyCode.LeftArrow;
} else if (Input.GetKeyDown (KeyCode.UpArrow) && dir != KeyCode.DownArrow) {
dir = KeyCode.UpArrow;
} else if (Input.GetKeyDown (KeyCode.DownArrow) && dir != KeyCode.UpArrow) {
dir = KeyCode.DownArrow;
}
//for testing character addition
else if (Input.GetKey (KeyCode.A)) {
addCharacter ();
}
}
void move() {
float x = 0;
float y = 0;
if (chars.Count != 0) {
//moves the transform in the appropriate directions
switch (dir) {
case KeyCode.RightArrow:
x = transform.position.x + moveUnits;
y = transform.position.y;
break;
case KeyCode.LeftArrow:
x = transform.position.x - moveUnits;
y = transform.position.y;
break;
case KeyCode.UpArrow:
x = transform.position.x;
y = transform.position.y + moveUnits;
break;
case KeyCode.DownArrow:
x = transform.position.x;
y = transform.position.y - moveUnits;
break;
default:
break;
}
//prevents him from moving outside the set boundaries
x = Mathf.Clamp (x, leftBoundary, rightBoundary);
y = Mathf.Clamp (y, bottomBoundary, topBoundary);
Vector2 pos = new Vector2 (x, y);
//this moves the whole snake
transform.position = pos;
//this moves the first snake segment
chars[0].transform.position = pos;
//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;
}
}
}
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<Character>());
}
}
}
推荐答案
更改FOR循环条件.从理论上讲,它将抛出超出范围的异常.
Change your FOR loop condition. Theoretically, it will throw out of range 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++)
chars [i].transform.position = chars [i - 1].transform.position;
我相信您的意图应该是
for (int i = chars.Count - 1; i > 0; i--)
这篇关于Unity C#ArgumentOutOfRangeException:参数超出范围.参数名称:索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!