0

I recently tried to change the code of one of my scripts of a game I'm working on in Unity and I keep on getting an argument out of range exception error message thrown at me for the ColouredBallPositions[x - 1] values I am adding in my for loop. However, the argument for this shouldn't be out of range.

This is the exact error message as displayed in the unity console:

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)

Ive tried to look through StackOverflow and the internet for answers to why I might receive this error message but I haven't found an answer to why my code would produce this error message.

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

}

No exception is thrown

Martin
  • 16,093
  • 1
  • 29
  • 48
Maurice Bekambo
  • 325
  • 6
  • 21

1 Answers1

2

I would think the problem here is that the index you are referring to does not exist.

You create ColouredBallPositions here:

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

The next time you use it is here:

ColouredBallPositions[x - 1] = (float)spawnPosX;

Although ColouredBallPositions is initialised, it is empty. At no point have you actually created anything with the index x - 1, so the index is out of range.

Martin
  • 16,093
  • 1
  • 29
  • 48