代码之家  ›  专栏  ›  技术社区  ›  Nitsha

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

  •  0
  • Nitsha  · 技术社区  · 9 年前

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

    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(子对象是应该激活的对象),记录它,并将其设置为激活状态。日志显示条件已满足,但不会启动gameObject。SetActive(真);命令

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

    2 回复  |  直到 9 年前
        1
  •  0
  •   Cenkisabi    9 年前

    首先,你找不到一个带有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
  •  0
  •   rkrishnasanka    9 年前

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

    尝试插入 gameObject = new WHATEVEROBJECTTHISIS();

    如果不是这样的话,你可以用更多的细节来扩展代码段吗。困扰我的是,如果对象为空,您应该会得到一个异常。我不知道为什么会发生这种情况。