Search code examples
unity-game-engineunity3d-editor

How can I acsess transform of child from a different gameobject


Im making an NPC wandering system and have baked the navmesh and all but i want to know how i can get the childs transform (i have aan epty gameobject eith all the destinations)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Apple;

public class PersonBehaviour : MonoBehaviour
{
    public GameObject destinationParent; // the empty game object

    private int chosenDestination;
    private GameObject destination;
    private int destinationCount;
    private Transform destinatonTransform;

    void Start()
    {
        destinationParent = GameObject.Find("Destinations");
        destinationCount = destinationParent.transform.childCount;
        chosenDestination = Random.Range(0, destinationCount + 1);
        //destinatonTransform = ???????


    }

}

Solution

  • If the chosenDestination is the index that you are searching for this could be a solution.

    destinationParent.transform.GetChild(chosenDestination).transform;