• 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
1
Question by raqbiel · Jun 27, 2019 at 11:08 AM · transformchildbounds

UnityException: Transform child out of bounds

Hello guys :)

Someone could help me fidn out where is the problem of UnityException: Transform child out of bounds? Code:

  bone = new Transform[gameObject.transform.childCount];
             bindPoses = new Matrix4x4[mesh.vertexCount];
             boneWeights = new BoneWeight[mesh.vertexCount];
             Vector3[] vertices = mesh.vertices;
             AssetDatabase.SaveAssets();
             Debug.Log("Bones: " + bone.Length + "BindBones: " + bindPoses.Length + " Weights: " + boneWeights.Length
                 );
 
             for (int index = 0; index <= mesh.vertexCount; index++)
             {
                 if(this.gameObject.transform.childCount > 0) { 
                 bone[index] = this.gameObject.transform.GetChild(index); < ----"UnityException: Transform child out of bounds"----->
                 bone[index].transform.position = this.transform.TransformPoint(mesh.vertices[index]);
 
                 bindPoses[index] = bone[index].transform.worldToLocalMatrix * this.transform.localToWorldMatrix;
                 boneWeights[index].boneIndex0 = index;
                 boneWeights[index].weight0 = 1;
                 Debug.Log(bone[index].name);
                 }
             }

Thanks for your time :)

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
1
Best Answer

Answer by rufopufo · Jun 27, 2019 at 11:48 AM

Hi there,

You are looping from 0 to the number of vertex in your mesh. It seems that your gameObject has got a different (less) number of children than vertexCount value.

Are you sure that your gameObject has as many children as number of vertex has the mesh?

If you want to make it work as it is... You have to change the condition in the "if" statement:

 if(this.gameObject.transform.childCount > index)

That way, you are avoiding the Exception, as you won't access a non-existing children.

Hope it helps.

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 raqbiel · Jun 27, 2019 at 12:00 PM 0
Share

Thanks you:) it solved the problem ;) But next appear.. :(

  mesh.RecalculateBounds();
             mesh.RecalculateNormals();
             mesh.RecalculateTangents();
             bone = new Transform[transform.childCount];
             bindPoses = new $$anonymous$$atrix4x4[mesh.vertexCount];
             boneWeights = new BoneWeight[mesh.vertexCount];
             Vector3[] vertices = mesh.vertices;
             AssetDatabase.SaveAssets();
             Debug.Log("Bones: " + bone.Length + "BindBones: " + bindPoses.Length + " Weights: " + boneWeights.Length + "VertexCount: " + mesh.vertexCount
                 );
 
             for (int index = 0; index <= mesh.vertexCount; index++)
             {
                 if (this.gameObject.transform.childCount > index)
                  bone[index] = this.gameObject.transform.GetChild(index);
                  bone[index].transform.position = this.transform.TransformPoint(mesh.vertices[index]); <----IndexOutOfRangeException: Array index is out of range. --->
 
                 bindPoses[index] = bone[index].transform.worldToLocal$$anonymous$$atrix * this.transform.localToWorld$$anonymous$$atrix;
                 boneWeights[index].boneIndex0 = index;
                 boneWeights[index].weight0 = 1;
                 Debug.Log(bone[index].name);
                 
             }
             mesh.bindposes = bindPoses;
             mesh.boneWeights = boneWeights;
             skinned.shared$$anonymous$$esh = mesh;
             skinned.bones = bones;
             mesh.vertices = vertices;
avatar image rufopufo raqbiel · Jun 28, 2019 at 05:53 AM 0
Share

Hi there,

You are missing your brackets in your if statement.

Just surround with brackets { } all the statements below "if".

 if (this.gameObject.transform.childCount > index) 
 {
                   bone[index] = this.gameObject.transform.GetChild(index);
                   bone[index].transform.position = this.transform.TransformPoint(mesh.vertices[index]); <----IndexOutOfRangeException: Array index is out of range. --->
  
                  bindPoses[index] = bone[index].transform.worldToLocal$$anonymous$$atrix * this.transform.localToWorld$$anonymous$$atrix;
                  boneWeights[index].boneIndex0 = index;
                  boneWeights[index].weight0 = 1;
                  Debug.Log(bone[index].name);
 }

You tabbed them correctly, but c# needs brackets.

Have a good coding.

avatar image
2

Answer by metalted · Jun 27, 2019 at 11:40 AM

It means you are trying to retrieve a child with a higher index than is available. As the index is incremented towards the mesh.vertexCount, make sure that the vertexCount isn't higher than the amount of children. You could place a check before the for loop so you know it will enter the for loop with the right data. Lets say childCount == 3, then childs(0) , (1) and (2) can be retrieved. Because of the [less or equal to sign] mesh.vertexCount in the for loop, the vertexCount can't be higher then 2. (I couldn't get the sign to display normally :P)

Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Child Transform Out of Bounds 1 Answer

Child gameobjects have incorrect offset (possibly caused by child world space canvas?) 0 Answers

Particles don't follow the transform of the parent gameobject? 0 Answers

Why is the child transform giving me errors? 2 Answers

Kinematic Child Object Jumping Above Player 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