Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

133 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 avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges