Im getting Index was out of range. Must be non negative and less than the size of the collection.

So im getting the error message and i’ve looked it at online and it says i need to declare how many spaces the list has. However i cant figure out where where to do that.

public int uran = 0, uranu= 0, child, childe;
public float cooldown = 10, warning_cooldwon = 5;
GameObject warning1, warning2, laser1, laser2;
new List<GameObject> warnings = new List<GameObject>();
new List<GameObject> lasers = new List<GameObject>();

// Start is called before the first frame update
void Start()
{
    laser_shot();
}
public void laser_shot()
{
    Debug.Log("notw");
    
    uran= Random.Range(0, 5);
    uranu= Random.Range(0, 5);
    child = Random.Range(0, 5);
    childe = Random.Range(0, 5);
    warning1 = warnings[uranu].gameObject;
    warning2 = warnings[uran].gameObject;
    laser1 = lasers[childe].gameObject;
    laser2 = lasers[child].gameObject;
    cooldown -= 0.1f;
    warning_cooldwon -= 0.05f;
    StartCoroutine(fire_laser());

}
// Update is called once per frame
void Update()
{
    foreach (GameObject yeet in GameObject.FindGameObjectsWithTag("Warning"))
    {

        warnings.Add(yeet);
    }
    foreach (GameObject yoot in GameObject.FindGameObjectsWithTag("Laser"))
    {

        lasers.Add(yoot);
    }
}

Any help is appreciated.

Hello. You need to debug the code while its running. To know where and when some index is out of range. Then track why is ot of range.

This error means, when code reads an array or a list it gets the error if the code tries to read some element that does not exist, i mean, if a list have 3 elements:

somelist[0]
somelist[1]
somelist[2]

If for some reason you try to read (or write) the value of an unexisting element

something = somelist[4];

It will give you “INDEX OUT OF RANGE”

This can happens because some list is empty, or you did a mistake in a for statement, something like that.

Normally is hard to see where is the error if you dont debug. As i said find where and when is trying to read an index that does not exist. If dont know how to debug while runnign, go youtube, is really very simple and easy and super necessary for coding.

BYE!

Hi,

A few things, you get that error because you are trying to access an index that doesn’t exist in your list. Not due to undefined size of the list. One of the advantages of lists is that you don’t have to define the size.

You are randomly selecting a range from 0-5 on your lists my guess is those lists doesn’t actually have that many items in. Normally you would select a random number based on the size of the list childe = Random.Range(0, lasers.Count); But your out of index is strange due to the below

Doing this call foreach (GameObject yeet in GameObject.FindGameObjectsWithTag("Warning"))

In each update is really expensive, and you should NOT do it at all, find another way. Also calling that for each and every update will add that object to the list every single frame if you are running at 60fps it will add 60 of each object to the list every second!.

The fact that you then get index out of range makes me think your Find with tags aren’t actually finding anything.

In short, you need to rethink the way you have this script setup as right now it is not fit for purpose

I think the problem is, that you’re trying to access an element of the list before there’s actually something in that list. The Lists “warnings” and “lasers” are empty at the start and in your Update function, you’re adding elemtents to those lists. The problem is, that Start() gets called before Update(), and therefore laser_shot() tries to find elements of an empty list. This gives you the Index out of Range error. Maybe try moving both foreach loops to the Start function, before you call laser_shot. This should ensure that there are Elements in your List, before you try to access a random one, assuming there are at least 6 GameObjects with the tag “Warning” and “Laser” in your Scene. Your Start function should look something like this:

 void Start()
 {
     foreach (GameObject yeet in GameObject.FindGameObjectsWithTag("Warning"))
     {
         warnings.Add(yeet);
     }
     foreach (GameObject yoot in GameObject.FindGameObjectsWithTag("Laser"))
     {
         lasers.Add(yoot);
     }

     laser_shot();
 }

And make sure to remove both foreach loops from Update(). As @MickyX mentioned, this is very performance heavy, and makes your lists GIGANTIC.