Search Unity

Transform child is out of bounds error

Discussion in 'Scripting' started by ChameleonPower3DDev, Jun 6, 2019.

  1. ChameleonPower3DDev

    ChameleonPower3DDev

    Joined:
    Mar 14, 2017
    Posts:
    24
    Hi everyone!

    I'm a 3D artist turned coder, thanks in advance for your patience. I'm working on a script that turns off UI panels on startup and am trying to add a check to see if they have children, if so, turn them off. I'm hitting this "out of bounds" error and I'm unsure why -- my logic is that I'm checking for children with the if statement, getting child number, and then setting that number of children to SetActive(false). Can anyone tell me what I'm missing?

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class StartupUIConfig : MonoBehaviour
    6. {
    7.     public GameObject[] PanelList;
    8.     public GameObject LandingPage;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         LandingPage.SetActive(true);
    13.         DisableAllPanels();
    14.     }
    15.     public void DisableAllPanels()
    16.     {
    17.         foreach (GameObject Panel in PanelList)
    18.         {
    19.             if(gameObject.transform.childCount > 0)
    20.             {
    21.                 var childPanels = gameObject.transform.childCount;
    22.                 gameObject.transform.GetChild(childPanels).gameObject.SetActive(false);
    23.             }
    24.             if (Panel != null)
    25.             {
    26.                 Panel.SetActive(false);
    27.             }
    28.         }
    29.     }
    30.  
    31. }
    32.  
    33.  
     
  2. ChameleonPower3DDev

    ChameleonPower3DDev

    Joined:
    Mar 14, 2017
    Posts:
    24
    I just figured it out -- I was getting an error from not having an int in the GetChild() method, so I assumed I had to find the number of children as a variable and place that in the method. Instead I deleted the variable and just added 0 and it works now. I'm still not sure as to why that works so if anyone doesn't mind explaining, I'd really appreciate knowing. Otherwise thanks and have a great day!
     
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You don't need to disable child object since they're disabled automagically when their parent is disabled. Unity handle that for you under the hood.
     
  4. ChameleonPower3DDev

    ChameleonPower3DDev

    Joined:
    Mar 14, 2017
    Posts:
    24
    Thanks for your reply palex-nx! You're right, but I'm working on a UI system right now and I only want specific child panels active when the parent is activated. For instance, my program has several pop-up panels that overlay all UI elements. This script is mostly for testing convenience but will most likely remain even after I'm done developing it.
     
  5. ChameleonPower3DDev

    ChameleonPower3DDev

    Joined:
    Mar 14, 2017
    Posts:
    24
    So it turns out that its not fixed -- the 0 worked because the array is 0 indexed and I only had 1 child panel turned on at the time. I accounted for the indexing with the variable and my error is gone, but the child panels are not deactivating. Does anyone have any insight for me? I'd very much appreciate it!

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class StartupUIConfig : MonoBehaviour
    6. {
    7.     public GameObject[] PanelList;
    8.     public GameObject LandingPage;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         LandingPage.SetActive(true);
    13.         DisableAllPanels();
    14.     }
    15.     public void DisableAllPanels()
    16.     {
    17.         foreach (GameObject Panel in PanelList)
    18.         {
    19.             var children = gameObject.transform.childCount;
    20.             for (int i = 0; i < children; i++)
    21.             {
    22.                 gameObject.transform.GetChild(children - 1).gameObject.SetActive(false);
    23.             }
    24.             if (Panel != null)
    25.             {
    26.                 Panel.SetActive(false);
    27.             }
    28.         }
    29.     }
    30. }
    31.  
    32.  
     
    akti86 likes this.
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    That's because you deactivating only last child in child list. Use i for index.