Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JJ_FX · Mar 12, 2019 at 11:29 AM · array

Problem with adding object to array

Hi,

I have an object with 8 children objects. And I need a function that returns only 6 of them, matching by name.

The problem is, when I declare an array of length 6, unity errors out that my array is not big enough:

 IndexOutOfRangeException: Index was outside the bounds of the array.

But when I set the length to 7 it works, but then I get 1 extra object null. This is my weird code:

 GameObject[] GetDetailChildren(GameObject Act_CubeMesh) {
     GameObject Houdini_TransformNode = Act_CubeMesh.transform.GetChild(0).gameObject.transform.GetChild(0).gameObject;
     Transform TR = Houdini_TransformNode.transform;
     GameObject[] arr = new GameObject[6];
     for (int i=0; i<TR.childCount; i++) {
         GameObject child = TR.GetChild(i).gameObject;
         if (child.name == "detail_Xminus0" ||
             child.name == "detail_Xplus0" ||
             child.name == "detail_Yminus0" ||
             child.name == "detail_Yplus0" ||
             child.name == "detail_Zminus0" ||
             child.name == "detail_Zplus0") {
             arr[i] = child;
         }
     }
     return arr;
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by xxmariofer · Mar 12, 2019 at 11:55 AM

because you have more childs that array length, you are trying to set one object at the position7 of the array when there is no position 7. you can just create a second var

 int j = 0;

and only increase j by one inside the if

     if (child.name == "detail_Xminus0" ||
          child.name == "detail_Xplus0" ||
          child.name == "detail_Yminus0" ||
          child.name == "detail_Yplus0" ||
          child.name == "detail_Zminus0" ||
          child.name == "detail_Zplus0") {
          arr[j] = child;
          j++;
      }
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 JJ_FX · Mar 12, 2019 at 01:11 PM 0
Share

omg u are right, a rookie mistake :D

avatar image
2

Answer by alikanat · Mar 12, 2019 at 11:59 AM

The problem is your code is lets say your "detail_Zplus0" is your 7th object you are trying to do this: arr[7] = child which causes the problem. Instead you can use a list like this:

 List<GameObject> GetDetailChildren(GameObject Act_CubeMesh)
     {
         GameObject Houdini_TransformNode = Act_CubeMesh.transform.GetChild(0).gameObject.transform.GetChild(0).gameObject;
         Transform TR = Houdini_TransformNode.transform;
         List<GameObject> arr = new List<GameObject>();
         for (int i = 0; i < TR.childCount; i++)
         {
             GameObject child = TR.GetChild(i).gameObject;
             if (child.name == "detail_Xminus0" ||
                 child.name == "detail_Xplus0" ||
                 child.name == "detail_Yminus0" ||
                 child.name == "detail_Yplus0" ||
                 child.name == "detail_Zminus0" ||
                 child.name == "detail_Zplus0")
             {
                 arr.Add(child);
             }
         }
         return arr;
     }


Now you do not have to deal with size.

Comment
Add comment · Show 2 · 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 JJ_FX · Mar 12, 2019 at 01:14 PM 0
Share

Oh this is nice too.

Are there any advantages to use lists ins$$anonymous$$d of arrays?

avatar image xxmariofer JJ_FX · Mar 12, 2019 at 01:22 PM 1
Share

array a bit better performance if you are not going to be changing the array elements much. list if you are going to be adding removing is better and easier to use (they can be resized)

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

118 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

Related Questions

calling a function when any variable in an array changes 1 Answer

Referencing arrays with a variable 1 Answer

Access array from one object to another. 1 Answer

Efficient wind on clouds 2 Answers

Array index out of range... but it is in range? 1 Answer


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