【问题标题】:How to activate a child of a parent object?如何激活父对象的子对象?
【发布时间】:2016-01-07 00:27:13
【问题描述】:

我有一个父对象,直到游戏后期才会激活。所以我只想在它进入触发器时激活。

using UnityEngine;
using System.Collections;

public class SetActiveOnTriggerEnter : MonoBehaviour {

    //public string FindGameOBJ; <-To be implemented for future re-usability. Ignore it for now.
   // public string ComponentToActivate; <-To be implemented for future re-usability. Ignore it for now.

    void OnTriggerEnter (Collider coll) // Collision detection.
    {
        if (coll.tag == "Ball") //Checks the tag to see if it is the PARENT OBJECT.
        {
                if (GameObject.Find("BallHealZone") == null) //Looks for the CHILD OBJECT, and if it is null than move forward.
                {
                    Debug.Log("Activating BallHealZone! :)"); //Tells me if it found said object, and confirms it is indeed null (yes it works).
                    gameObject.SetActive(true); //Activates the CHILD OF PARENT OBJECT.
                }
        }
    }

}

基本上如您所见,它会检查标签是否正确找到游戏对象(孩子是应该被激活的),记录它,并应该将其设置为活动状态。日志说条件得到满足,但它不会触发 gameObject.SetActive(true);命令。

如何激活父对象的子对象?

【问题讨论】:

    标签: c# unity3d parent-child


    【解决方案1】:

    首先,您无法使用 GameObject.Find() 方法找到已停用的游戏对象。因为 GameObject.Find() 找到并返回“gameObject”,但您的场景中没有具有此名称的游戏对象。你应该找到这个游戏对象的变换。所以如果你想激活子对象,试试这个。

    (我认为这个脚本附加到父对象)

    using UnityEngine;
    using System.Collections;
    
    public class SetActiveOnTriggerEnter : MonoBehaviour {
    
        //public string FindGameOBJ; <-To be implemented for future re-usability. Ignore it for now.
        // public string ComponentToActivate; <-To be implemented for future re-usability. Ignore it for now.
    
        void OnTriggerEnter (Collider coll) // Collision detection.
        {
            if (coll.tag == "Ball") //Checks the tag to see if it is the PARENT OBJECT.
            {
                    if (!transform.FindChild("BallHealZone").gameObject.activeInHierarchy) //This is better form for looking , but there is a one better way, GetChild. You can see it on below. 
                    {
                        Debug.Log("Activating BallHealZone! :)"); //Tells me if it found said object, and confirms it is indeed null (yes it works).
                        transform.FindChild("BallHealZone").gameObject.SetActive(true); //Activates the CHILD OF PARENT OBJECT.
                        //or if you know index of child in parent you can use GetChild method for a faster one
                        transform.GetChild(indexOfChild).gameObject.SetActive(true); // this also activates child, but this is faster than FindChild method
                    }
            }
        }
    }
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      从外观上看,GameObject.Find("BallHealZone") 返回对 gameObject 的引用,如果 gameObject 是 null,这意味着尚未创建此对象的引用。通常这意味着您在执行方法之前实例化对象,除非它是静态方法。

      尝试插入gameObject = new WHATEVEROBJECTTHISIS();

      如果不是这种情况,您能否用更多细节扩展代码 sn-p。困扰我的是,如果对象为空,你应该得到一个异常。我不知道为什么会发生这种情况。

      【讨论】:

      • 它没有试图实例化任何东西...代码等待带有正确标签的东西进入,寻找游戏对象,并检查它是否为空。如果它是 null (它是)它返回一个 debug.log (它正在工作),并且应该激活子对象(它没有做)。
      • 我已经用 cmets 更新了我的问题代码 sn-p 以更好地解释正在(或应该)发生的事情。
      猜你喜欢
      • 2017-12-08
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2021-01-25
      相关资源
      最近更新 更多