• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fonics · Jul 16, 2020 at 06:02 PM · indexarraylistindexoutofrangeexception

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);
                 }
             }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fonics · Jul 18, 2020 at 03:38 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling Game object from Element list? 0 Answers

Array index is out of range error Edited 1 Answer

Update/Replace value in ArrayList 1 Answer

Identify an array index of a gameobject when clicked and interact with the selected gameobject(index) individually 2 Answers

IndexOutOfRangeException 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges