- Home /
IndexOutOfRangeException: Index was outside the bounds of the Array
I'm still relatively new to unity, I'm trying to create a script to handle weapon information (like ammunition, damage, name etc) and handle weapon switching. My script is giving me the above error but not providing a line so I don't know where it is occuring
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class weaponManager : MonoBehaviour
{
public GameObject weaponHolder;
public Transform[] weaponHolderChildren = new Transform[9];
public List<GameObject> weaponObjects = new List<GameObject>(9);
public List<weaponClass> weapons = new List<weaponClass>(9);
public GameObject testObject;
int currentWeapon;
void Start()
{
weaponHolderChildren = GetComponentsInChildren<Transform>();
for (int i = 0; i < weaponObjects.Count; i++)
{
weaponObjects.Add(weaponHolderChildren[i].gameObject);
}
int currentIndex = 0;
foreach (GameObject weaponGameObject in weaponObjects){
weapons[currentIndex].weaponObject = weaponGameObject;
weapons[currentIndex].weaponName = weaponGameObject.GetComponent<weaponStats>().weaponName;
weapons[currentIndex].damage = weaponGameObject.GetComponent<weaponStats>().damage;
weapons[currentIndex].maxAmmo = weaponGameObject.GetComponent<weaponStats>().maxAmmo;
weapons[currentIndex].ammoPickupSize = weaponGameObject.GetComponent<weaponStats>().ammoPickupSize;
weapons[currentIndex].ammoPickupName = weaponGameObject.GetComponent<weaponStats>().ammoPickupName;
currentIndex++;
}
currentWeapon = 0;
setActiveWeapon(currentWeapon);
}
void Update()
{
}
public void testForWeaponInput()
{
}
public void setActiveWeapon(int passedWeapon)
{
int currentIndex = 0;
foreach (weaponClass weapon in weapons)
{
if (currentIndex == passedWeapon)
{
weapon.weaponObject.SetActive(true);
}
else
{
weapon.weaponObject.SetActive(false);
}
}
}
}
Answer by Elango · Jul 17, 2020 at 08:07 AM
You loop endlessly through the list and add new items to it. You end up trying to get the item at index 9+ from weaponHolderChildren which should only have 9 items (max index = 8).
for (int i = 0; i < weaponObjects.Count; i++)
{
weaponObjects.Add(weaponHolderChildren[i].gameObject);
}
Since you designated the capacity of the list, you probably wanted to reassign existing items rather than add new ones.
for (int i = 0; i < weaponHolderChildren.Length; i++)
{
weaponObjects[i] = (weaponHolderChildren[i].gameObject);
}
And keep in mind that your code is designed only for the same number of elements in all arrays (weaponHolderChildren, weaponObjects, weapons). It's easy to get another out of range error. Aslo
GetComponentsInChildren<Transform>()
include parent too.
Thank you for your help! I ended up making this significantly more efficient only using one list, but your tip about GetComponentsInChildren including the parent was lifesaving.
Your answer
![](https://koobas.hobune.stream/wayback/20220613003330im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Hashtable and ArrayList problem 0 Answers
IndexOutOfRangeException: Array index is out of range. 0 Answers
Index out of bound when trying to select a character 1 Answer
What means this error ? 1 Answer
Calling Game object from Element list? 0 Answers