如何从不同的游戏对象中访问子对象的变换?

huangapple go评论54阅读模式
英文:

How can I acsess transform of child from a different gameobject

问题

我正在制作一个NPC漫游系统,并已经烘焙了导航网格,但我想知道如何获取子物体的变换(我有一个空的游戏对象,其中包含所有的目的地)。

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

public class PersonBehaviour : MonoBehaviour
{
    public GameObject destinationParent; // 空的游戏对象

    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 = 如何获取子物体的变换?


    }
}

请注意,你需要补充如何获取子物体的变换的代码。

英文:

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 = ???????


    }

}

答案1

得分: -1

如果 chosenDestination 是你正在搜索的索引,这可能是一个解决方案。

destinationParent.transform.GetChild(chosenDestination).transform;
英文:

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

destinationParent.transform.GetChild(chosenDestination).transform;

huangapple
  • 本文由 发表于 2023年7月18日 00:16:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定