在不应该有范围异常的情况下继续接收范围异常的参数?
Keep on receiving an argument of of range exception when there shouldn't be one?
我最近试图更改我在 Unity 中开发的游戏脚本之一的代码,但我一直收到针对 ColouredBallPositions[x - 1]
向我抛出的参数超出范围异常错误消息我在 for 循环中添加的值。但是,此参数不应超出范围。
这是统一控制台中显示的确切错误消息:
ArgumentOutOfRangeException: Index was out of range. Must be
non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException
(System.ExceptionArgument argument, System.ExceptionResource resource)
(at <23c160f925be47d7a4fd083a3a62c920>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at
<23c160f925be47d7a4fd083a3a62c920>:0)
System.Collections.Generic.List`1[T].set_Item (System.Int32 index, T
value) (at <23c160f925be47d7a4fd083a3a62c920>:0) SpawnPoint.Start ()
(at Assets/SpawnPoint.cs:43)
我试图通过 Whosebug 和互联网寻找我可能会收到此错误消息的答案,但我还没有找到我的代码为何会产生此错误消息的答案。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnPoint : MonoBehaviour
{
public Transform SpawnPoints;
List<float> StarPositions = new List<float>();
List<float> ColouredBallPositions = new List<float>();
List<float> ColouredBallRanges = new List<float>();
int Interact, randomSpawnPoint;
public static bool spawnAllowed;
ObjectPooler objectPooler;
int index = 1;
public void Start()
{
objectPooler = ObjectPooler.Instance;
if (ScoreScript.scoreValue < 5)
{
Vector2 pos =
Camera.main.WorldToViewportPoint(transform.position);
for (int x = 1; x < 5; x++) // Change this to get rid of the need to generate spawnpoints
{
//Vector3 SpawnPos = spawnPoints[d].position;
int NrSpawnpoints = 4;
int NrSpaces = NrSpawnpoints + 1;
double Xlegnth = 1.0;
double spawnPosX = x * Xlegnth / NrSpaces;
pos.x = (float)spawnPosX;
pos.y = 1.3f;
ColouredBallPositions[x - 1] = (float)spawnPosX;
Vector2 Posi = Camera.main.ViewportToWorldPoint(pos);
Instantiate(SpawnPoints, Posi, Quaternion.identity);
// Debug.Log(Posi);
}
spawnAllowed = true;
InvokeRepeating("SpawnAInteract", 0f, 1f);
}
}
没有抛出异常
我认为这里的问题是您所指的索引不存在。
您在此处创建 ColouredBallPositions
:
List<float> ColouredBallPositions = new List<float>();
下次使用就在这里:
ColouredBallPositions[x - 1] = (float)spawnPosX;
虽然ColouredBallPositions
被初始化了,但是空。您实际上从未使用索引 x - 1
创建任何内容,因此索引超出范围。
我最近试图更改我在 Unity 中开发的游戏脚本之一的代码,但我一直收到针对 ColouredBallPositions[x - 1]
向我抛出的参数超出范围异常错误消息我在 for 循环中添加的值。但是,此参数不应超出范围。
这是统一控制台中显示的确切错误消息:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <23c160f925be47d7a4fd083a3a62c920>:0) System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <23c160f925be47d7a4fd083a3a62c920>:0) System.Collections.Generic.List`1[T].set_Item (System.Int32 index, T value) (at <23c160f925be47d7a4fd083a3a62c920>:0) SpawnPoint.Start () (at Assets/SpawnPoint.cs:43)
我试图通过 Whosebug 和互联网寻找我可能会收到此错误消息的答案,但我还没有找到我的代码为何会产生此错误消息的答案。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnPoint : MonoBehaviour
{
public Transform SpawnPoints;
List<float> StarPositions = new List<float>();
List<float> ColouredBallPositions = new List<float>();
List<float> ColouredBallRanges = new List<float>();
int Interact, randomSpawnPoint;
public static bool spawnAllowed;
ObjectPooler objectPooler;
int index = 1;
public void Start()
{
objectPooler = ObjectPooler.Instance;
if (ScoreScript.scoreValue < 5)
{
Vector2 pos =
Camera.main.WorldToViewportPoint(transform.position);
for (int x = 1; x < 5; x++) // Change this to get rid of the need to generate spawnpoints
{
//Vector3 SpawnPos = spawnPoints[d].position;
int NrSpawnpoints = 4;
int NrSpaces = NrSpawnpoints + 1;
double Xlegnth = 1.0;
double spawnPosX = x * Xlegnth / NrSpaces;
pos.x = (float)spawnPosX;
pos.y = 1.3f;
ColouredBallPositions[x - 1] = (float)spawnPosX;
Vector2 Posi = Camera.main.ViewportToWorldPoint(pos);
Instantiate(SpawnPoints, Posi, Quaternion.identity);
// Debug.Log(Posi);
}
spawnAllowed = true;
InvokeRepeating("SpawnAInteract", 0f, 1f);
}
}
没有抛出异常
我认为这里的问题是您所指的索引不存在。
您在此处创建 ColouredBallPositions
:
List<float> ColouredBallPositions = new List<float>();
下次使用就在这里:
ColouredBallPositions[x - 1] = (float)spawnPosX;
虽然ColouredBallPositions
被初始化了,但是空。您实际上从未使用索引 x - 1
创建任何内容,因此索引超出范围。