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

Resolved Mask or fix intentional out of bounds error

Discussion in 'Scripting' started by Shepherd-of-fire, Nov 3, 2020.

  1. Shepherd-of-fire

    Shepherd-of-fire

    Joined:
    Oct 9, 2020
    Posts:
    4
    Hello, i'm trying to put all the chairs, under the GameObject this script is attached to, into a list. I want to make the amount of chairs vary, so i can't drag them into the list in Unity.
    when i run the game it says:

    UnityException: Transform child out of bounds
    ChairRegistry.Start () (at Assets/Scripts/ChairRegistry.cs:13)

    I know it would be out of bounds, that's when i want the script to stop. And it's not a major issue, but the more i have of these errors, the harder it will be to find the actual errors.

    Help would be much appriciated

    the code i have problems with is the following:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChairRegistry : MonoBehaviour
    6. {
    7.     public List<GameObject> chairObject;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         int chairCount = 0;
    13.         while ( transform.GetChild(chairCount).transform)
    14.         {
    15.             GameObject objectfound = transform.GetChild(chairCount).gameObject;
    16.             chairObject.Add(objectfound);
    17.             chairCount += 1;
    18.         }
    19.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    There are much better ways to loop through the children of a transform than intentionally throwing an exception! Here's a couple examples:
    Code (CSharp):
    1. foreach (Transform child in transform) {
    2.   GameObject objectfound = child.gameObject;
    3.   chairObject.Add(objectfound);
    4.   // etc...
    5. }
    Code (CSharp):
    1. for (int i = 0; i < transform.childCount; i++) {
    2.   Transform child = transform.GetChild(i);
    3.   GameObject objectfound = child.gameObject;
    4.   chairObject.Add(objectfound);
    5.   // etc...
    6. }
     
  3. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    You can catch errors and stop them with a try .. catch block, but relying on this as a way of terminating your loop is generally a bad idea when you have other options. (It's bad because it'll also "catch" unintentional exceptions, causing things to fail silently.) And you do have other options!

    If your chairs have a script on them (and, if they're being used as participants in the game as they seem to be, they probably should), use GetComponentsInChildren<YourChairScript>(), and loop through the array that returns. (Adding a script to your chairs won't cause performance problems as long as you remove the empty Start() and Update() methods from it)

    This also will loop through all children of a transform, but I'd recommend this only if the above isn't an option for some reason - if you ever add non-chair objects into the hierarchy, that will mess up this approach.
    Code (csharp):
    1. foreach (Transform child in transform) {
    2. chairObject.Add(child.gameObject);
    3. }
     
  4. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    Code (CSharp):
    1.       while (chairCount <transform.childCount)
    2.         {
    3.             GameObject objectfound = transform.GetChild(chairCount).gameObject;
    4.             chairObject.Add(objectfound);
    5.             chairCount += 1;
    6.         }
     
  5. Shepherd-of-fire

    Shepherd-of-fire

    Joined:
    Oct 9, 2020
    Posts:
    4
    Ty for your response! <3