• 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 solutionlab · Aug 07, 2019 at 10:54 AM · transformchildout of bounds

Transform child out of bounds

Hey Guys, I have some trouble with the error message "transform child out of bounds".... In my project I have a switch-case to look which objcet has been choosen and what the user wants to do with it. The object will be initiated via mouse click.

Case 1 works perfectly but case 2 shows the error message... Hopefully, I can get some help :-)

Here is my code:

     findMychild = transform.Find("WeldContent/Schweißnaht " + ButtonActionManager.PosValue).gameObject;
     
             //Referenz erzeugen zur Klasse SavePath um die Funktion RemoveListElements aufzurufen
             GameObject gone = GameObject.Find("StreamingClient/Table_KS/TCP");
             SavePath vas = (SavePath)gone.GetComponent(typeof(SavePath));
     
             if (ButtonActionManager.ButtonStatus == false)
             {
     
                 //Debug.Log("Status: false");
                 switch (ButtonActionManager.PosValue) //DropdownPrefab.value
                 {
                     //Default -Bitte Auswählen-
                     case 0:
                         Debug.Log("keine Auswahl getroffen");
                         break;
     
                     //Auswahl 1
                     case 1:
                         Debug.Log("Auswahl 1");
                         //Wenn der Button für Highlight gedrückt wurde
                         if (ButtonInteraction.Highlight == true)
                         {
                             if (Color == true)
                             {
                                 Debug.Log("Coloer == true");
                                 for (int k = SavePath.Trajektory.Count - 1; k >= 0; k--)
                                 {
                                     if (SavePath.Trajektory[k].SchweißNahtNo == ButtonActionManager.PosValue)
                                     {
                                         rend = findMychild.transform.GetChild(k).GetComponent<MeshRenderer>();
                                         rend.sharedMaterial = Material;
                                         
                                     }
                                 }
                                 Debug.Log("Set Color false");
                                 Color = false;
                             }
                             else if (Color == false)
                             {
                                 Debug.Log("Coloer == false");
                                 for (int k = SavePath.Trajektory.Count - 1; k >= 0; k--)
                                 {
                                     if (SavePath.Trajektory[k].SchweißNahtNo == ButtonActionManager.PosValue)
                                     {
                                         rend = findMychild.transform.GetChild(k).GetComponent<MeshRenderer>();
                                         rend.sharedMaterial = OtherMaterial;
     
                                     }
                                 }
                                 Debug.Log("Set Color true");
                                 Color = true;
                             }
                             
                         }
     
                         //Wenn Auswahl Delete getroffen ist dann destroy parent GameObject
                         if (ButtonInteraction.Delete == true)
                         {
                             Debug.Log("Auswahl Delete getroffen");
                             //aufrufen der Funktion RemoveListElements
                             vas.RemoveListElements();
     
                             Debug.Log("findMychild zerstören");
                             //Zerstören des GameObjects findMychild
                             Destroy(findMychild);
                         }
     
                         //Wenn Auswahl Activate/Deactivate getroffen ist Activiere/Deaktiviere das GameObject
                         if (ButtonInteraction.Visibility == true)
                         {
                             findMychild.SetActive(false);
                         }
                         break;
     
     



case 2 is excacly the same!

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
0

Answer by georgelewising · Aug 07, 2019 at 11:15 AM

The error message means that it's looking for an index beyond the length of the number of children. E.G. Perhaps it's looking for child number "5" when findMyChild has only 4 children.


You can verify this by comparing the lengths of SavePath.Trajektory.Count and findMychild.transform.childCount. If the former is longer than the latter then it'll throw your error!

Comment
Add comment · Show 4 · 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 solutionlab · Aug 07, 2019 at 11:45 AM 0
Share

Thanks for the very fast answer! So Trajektory.Count is always larger than childCount because Trajektory is a list containing all initiated objects.

The idea is very simple: I want to select all objects with a specific attribute. The objects get the attribute when they are created. When I am holding the mouse button I can create more than one object but it has the same attribute.

Now I want to select all objects with 1 and change its colour. So all I do is to look into my list and searching for all objects with attribute 1 and when I find it I am changing its colour.

Do I have an issue in my logic?

avatar image georgelewising solutionlab · Aug 07, 2019 at 12:28 PM 1
Share

Gotcha! And you do!

If you imagine you have 100 objects in your Trajektory array but only 5 children in the transform then k = 100 in the first loop, so you're performing: rend = find$$anonymous$$ychild.transform.GetChild(100);

That's looking for the 100th child of find$$anonymous$$yChild, when it only has 5 children. Oops!

If you want to set all of the children to use the $$anonymous$$aterial then replace that line with a new loop using a variable other than k!

avatar image solutionlab georgelewising · Aug 07, 2019 at 01:37 PM 0
Share

so the best thing is to find out how large find$$anonymous$$ychild is and set this number as variable. Thank´s a lot for your help!

Show more comments
avatar image
0

Answer by solutionlab · Aug 08, 2019 at 01:16 PM

So the solution was to take "findMychild.transform.childCount" as variable in the loop :-)

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 georgelewising · Aug 08, 2019 at 01:20 PM 0
Share

$$anonymous$$akes sense! Glad it all worked out for you.

Worth marking the question as closed :)

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

140 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

Related Questions

Child Transform 1 Answer

Make a GameObject child to a "Dragged Reference" GameObject doesn't work! 0 Answers

Parent object is getting generated why ?? 1 Answer

How can I access the children of a Transform? 5 Answers

Mesh Collider Does not rotate with Mesh 2 Answers

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