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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Transform child out of bounds

Discussion in 'Scripting' started by Invader_, Mar 27, 2017.

  1. Invader_

    Invader_

    Joined:
    Mar 12, 2017
    Posts:
    10
    Code (CSharp):
    1. Debug.developerConsoleVisible = true;
    2. GameObject ChildGameObject1 = transform.GetChild(0).gameObject; // throws exception: Transform child out of bounds
    3. Debug.Log(transform.childCount); // debugs 1
    4. Debug.Log(ChildGameObject1.name); // debugs Cube, and thats the name of the child...
    wtf is going on here the script is applied to the parent of the cube???

    thx very much
     
    hopetolive likes this.
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    Looks like a bug! Report it through Help->Report a bug, be sure to include a repro project.
     
  3. Invader_

    Invader_

    Joined:
    Mar 12, 2017
    Posts:
    10
    This show it even better:

    Code (CSharp):
    1.  
    2. private void OnDrawGizmos()
    3. {
    4. Debug.developerConsoleVisible = true;
    5. Debug.Log("000:"+transform.childCount);
    6. GameObject ChildGameObject1 = transform.GetChild(0).gameObject;
    7. Debug.Log("001:"+transform.childCount);
    8. }
    9.  
    000:1
    001:1
    000:0
    UnityException: Transform child out of bounds

    this is pretty gg for me ^^ I cannot wait until unity saves the bug if it is one :(

    It seems like GetChild takes the child away instead of returning a reference or so...

    anyway i reported it
     
    prasenkakade21 likes this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    If that is the four lines of output you get, it looks like you've got two instances of the script in the scene, one which has a child and one which doesn't.
     
    hopetolive and amit-chai like this.
  5. Invader_

    Invader_

    Joined:
    Mar 12, 2017
    Posts:
    10
    thx you are right. Sry im new to unity :D
     
  6. YoyoMario

    YoyoMario

    Joined:
    Apr 8, 2013
    Posts:
    15
    Not a bug...
    He is trying to get a non existant gameObject, he needs to assign it via conditional operator, if statement or check for it via exception....
     
    ryo310 likes this.
  7. vivaanninja

    vivaanninja

    Joined:
    Jun 2, 2021
    Posts:
    1
    I'm getting the same problem.. but with photon spawning
    here's the code -

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon;
    5. using Photon.Pun;
    6. using Photon.Realtime;
    7. using UnityEngine.UI;
    8.  
    9. public class PhotonManager : MonoBehaviourPunCallbacks
    10. {
    11.     public GameObject LobbyManager;
    12.     public GameObject loginAndRegisterPanels;
    13.     public GameObject ConnectPanel;
    14.     public GameObject Canvas;
    15.     public GameObject[] SpawnPoints;
    16.     public override void OnConnectedToMaster() {
    17.         loginAndRegisterPanels.SetActive(false);
    18.         ConnectPanel.SetActive(false);
    19.         LobbyManager.SetActive(true);
    20.         PhotonNetwork.JoinOrCreateRoom("test", new RoomOptions {IsOpen = true}, TypedLobby.Default);
    21.     }
    22.     public override void OnJoinedRoom() {
    23.         Canvas.SetActive(false);
    24.         PhotonNetwork.Instantiate("Player", SpawnPoints[Random.Range(0, SpawnPoints.Length)].transform.GetChild(0).position, Quaternion.identity, 0);
    25.     }
    26. }
    27.  
    edit: I fixed it. the pos I wanted to spawn at did NOT have the index of 0
     
    Last edited: Jun 21, 2021