Need help with ArgumentOutOfRange Error

I’m currently getting this error.

ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[UnityEngine.GameObject].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
Gaskin_G015877f.Controllers.NewCombatAI.combatAction () (at Assets/Mods/Gaskin_G015877f/NewCombatAI.cs:220)
NoxCore.Controllers.AIStateController.processState () (at Assets/Custom Scripts/Controllers/AI/AIStateController.cs:111)
NoxCore.Controllers.AIStateController+<update>c__Iterator0.MoveNext () (at Assets/Custom Scripts/Controllers/AI/AIStateController.cs:85)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

From this code.

if (structure.scanner.isNewSweep() == true)
                        {
                            Gui.setMessage("scanning");

                            frontTargets.Clear();
                            shieldedTargets.Clear();
                            potentialMissileTargets.Clear();

                            // group1 finds a target
                            List<Weapon> weapons = group1.getAllWeapons();
                            Weapon weap1 = weapons[2];

                            foreach (GameObject target1 in structure.scanner.getEnemiesInRange())
                            {
                                TurretSocket turretSocket = weap1.Socket as TurretSocket;

                                Vector3 pos = turretSocket.transform.position;
                                Vector3 forward = turretSocket.transform.TransformDirection(0, 1, 0);

                                if (Vector3.Dot(forward, (target1.transform.position - pos)) >= Mathf.Cos(turretSocket.fireArcHalf))
                                {
                                    frontTargets.Add(target1);
                                }
                            }

                            group1.setTarget(frontTargets[0]);
                            // group1 stops here

                            //group2 finds a target
                            if (missileTarget == null)
                            {
                                foreach (GameObject target2 in structure.scanner.getEnemiesInRange())
                                {
                                    if (Vector3.Distance(target2.transform.position, transform.position) <= 1000)
                                    {
                                        potentialMissileTargets.Add(target2);
                                    }
                                }

                                missileTarget = potentialMissileTargets[0];

                            }
                            else
                            {
                                if (ammoRequired == 0)
                                {
                                    ammoRequired = (missileTarget.GetComponent<Structure>().MaxHullStrength / 100);
                                }

                                group2.setTarget(missileTarget);
                            }                           
                            //group2 stops here

                            //group3 finds a target
                            foreach (GameObject target3 in structure.scanner.getEnemiesInRange())
                            {
                                if (target3.GetComponent<StaticTarget>() != null)
                                {
                                    if (target3.GetComponent<StaticTarget>().AllShieldsFailed != true)
                                    {
                                        shieldedTargets.Add(target3);
                                        //Gui.setMessage("found shielded target");
                                    }
                                }

                                if (shieldedTargets.Count > 0)
                                {
                                    group3.setTarget(shieldedTargets[0]);
                                }
                            }

                            //group3 stops here

from what I understand its to do with a list somewhere not having any content but I’m not sure which one. the lists here aren’t manipulated anywhere else within the code so something here is causing the issue.

Any help would be appreciated.

Hello there,

The error you are getting means that you are trying to grab something from a specific position in a List, yet that position doesn’t exist because the List is too short. For example, you may be trying to grabd the 5th item a a List that’s only 3 items long.

As a general rule, you should avoid getting an item from a list by giving it a magic (understand arbitrary) number. At the beginning of this sample for example, you use Weapon weap1 = weapons[2]; You are trying to grab the 3rd element, but that list might not have that many items inside it, which would output your error.

An easy way to find out is to debug it: Try throwing the line Debug.Log(weapons.Count); in there, right before Weapon weap1 = weapons[2];. If you don’t have at least “3”, then that’s your issue.
Also, when creating a new List, I’ve had some issues with the “=” operator in the past. You might want to try using List<Weapon> weapons = new List<Weapon>(group1.getAllWeapons()); instead.

Same thing a little lower, you are doing missileTarget = potentialMissileTargets[0];, yet that collection might be empty. You should debug it as well.

I hope that helps!

~LegendBacon