【发布时间】: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