Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why I keep getting exception UnityException: Transform child out of bounds ?

Discussion in 'Scripting' started by dubiduboni_unity, Mar 24, 2019.

  1. dubiduboni_unity

    dubiduboni_unity

    Joined:
    Feb 11, 2019
    Posts:
    116
    Code (csharp):
    1.  
    2. List<Transform> children = new List<Transform>();
    3.  
    4.         for (int i = 0; i < doors.Count; i++)
    5.         {
    6.             foreach (Transform child in doors[i].GetComponentsInChildren<Transform>())
    7.             {
    8.                 children.Add(child.GetChild(0));
    9.             }
    10.         }
    11.  
    There are two doors. Door_Left and Door_Right
    Each door have a child. Door_Left child name is DoorShieldFXLocked (1)
    And Door_Right child name is DoorShieldFXLocked

    I'm trying to add both DoorShieldFXLocked (1) and DoorShieldFXLocked to the List children
    when using break point I see that it's adding the first DoorShieldFXLocked (1) but on the next loop when it stop on the line:

    Code (csharp):
    1.  
    2. children.Add(child.GetChild(0));
    3.  
    I'm getting the exception:

    UnityException: Transform child out of bounds
    At HoriDoorManager.cs:60

    Line 60 is:

    Code (csharp):
    1.  
    2. children.Add(child.GetChild(0));
    3.  
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    You're already itterating through the children using
    Code (CSharp):
    1. foreach (Transform child in doors[i].GetComponentsInChildren<Transform>())
    Add the children using
    Code (CSharp):
    1. children.Add(child);
    The reason your code isn't working is because GetComponentsInChildren actually gets all the child transforms.
     
    dubiduboni_unity likes this.
  3. dubiduboni_unity

    dubiduboni_unity

    Joined:
    Feb 11, 2019
    Posts:
    116
    The problem now is that it's also adding to the children List the parents:

    Code (csharp):
    1.  
    2. List<Transform> children = new List<Transform>();
    3.  
    4.         for (int i = 0; i < doors.Count; i++)
    5.         {
    6.             foreach (Transform child in doors[i].GetComponentsInChildren<Transform>())
    7.             {
    8.                 children.Add(child);
    9.             }
    10.         }
    11.  
    It should add to children only the child of each door. Each door have one child and there are two doors.
    So in the end in children there should be 2 items. But there are 4 also the two doors.

    How can I add only the child of each door without the parents(doors) ?
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    GetComponentsInChildren returns objects found in the object you are searching and its children. So, just skip the parent;

    Code (CSharp):
    1.         List<Transform> children = new List<Transform>();
    2.  
    3.         for (int i = 0; i < doors.Count; i++)
    4.         {
    5.             foreach (Transform child in doors[i].GetComponentsInChildren<Transform>())
    6.             {
    7.                 if (child == doors[i].transform)
    8.                     continue;
    9.  
    10.                 children.Add(child);
    11.             }
    12.         }
     
    dubiduboni_unity likes this.