C# 当不应出现'时,继续接收范围异常的参数;不是吗?

C# 当不应出现'时,继续接收范围异常的参数;不是吗?,c#,list,unity3d,outofrangeexception,C#,List,Unity3d,Outofrangeexception,我最近试图更改我在Unity中开发的一个游戏脚本的代码,我不断收到一条参数超出范围的异常错误消息,该消息针对我在for循环中添加的coloredBallPositions[x-1]值。然而,这一观点不应该超出范围 这是unity控制台中显示的确切错误消息: ArgumentOutOfRangeException:索引超出范围。必须是 非负数且小于集合的大小 参数名称:索引 System.ThrowHelper.ThrowarGumentoFrangeException (System.Excep

我最近试图更改我在Unity中开发的一个游戏脚本的代码,我不断收到一条参数超出范围的异常错误消息,该消息针对我在for循环中添加的
coloredBallPositions[x-1]
值。然而,这一观点不应该超出范围

这是unity控制台中显示的确切错误消息:

ArgumentOutOfRangeException:索引超出范围。必须是 非负数且小于集合的大小

参数名称:索引 System.ThrowHelper.ThrowarGumentoFrangeException (System.ExceptionArgument参数,System.ExceptionResource) (at:0) System.ThrowHelper.throwargumentotofrangeexception()(位于 :0) System.Collections.Generic.List`1[T]。设置项(System.Int32索引,T 值)(在:0)点。开始() (资产/利润点:43)

我试图通过StackOverflow和internet查找为什么我可能会收到此错误消息的答案,但我还没有找到为什么我的代码会产生此错误消息的答案

    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);
    }

}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类繁殖点:单行为
{
公共交通点;
列表起始位置=新列表();
List coloredBallPositions=新列表();
List coloredBallRanges=新列表();
int交互,随机生成点;
允许公共静态布尔值;
ObjectPooler ObjectPooler;
int指数=1;
公开作废开始()
{
objectPooler=objectPooler.Instance;
if(ScoreScript.scoresvalue<5)
{
矢量2位置=
Camera.main.WorldToViewportPoint(变换位置);
for(int x=1;x<5;x++)//更改此选项以消除生成生成点的需要
{
//向量3 SpawnPos=spawnPoints[d]。位置;
积分=4;
int NrSpaces=nrsownpoints+1;
双Xlegnth=1.0;
double spawnPosX=x*Xlegnth/nr空间;
pos.x=(浮动)posx;
位置y=1.3f;
彩色球位[x-1]=(浮动)PosX;
Vector2 Posi=Camera.main.ViewportToWorldPoint(pos);
实例化(SpawnPoints、Posi、四元数.identity);
//Debug.Log(Posi);
}
允许为真;
调用重复(“SpawnAInteract”,0f,1f);
}
}

没有引发异常

我认为这里的问题是您所引用的索引不存在

您可以在此处创建
彩色球位

List<float> ColouredBallPositions = new List<float>();

虽然
coloredballpositions
已初始化,但它是空的。您实际上从未使用索引
x-1创建过任何内容,因此索引超出范围。

我认为这里的问题是您所指的索引不存在

您可以在此处创建
彩色球位

List<float> ColouredBallPositions = new List<float>();

虽然
coloredballpositions
是初始化的,但它是空的。你从来没有用索引
x-1
创建过任何东西,所以索引超出了范围。

谢谢,伙计,我意识到我的错误是什么。把列表当作数组来处理,我意识到我的错误是什么。把列表当作数组来处理