当区域内的敌人被杀时,我想激活/启用TriggerCollider。敌人没有按某种顺序排列,因此无法向单个敌人添加脚本。
我创建了一个空的游戏对象并附加在下面(错误的)脚本。我想做if( the public game objects are destroyed) activate trigger
。
你知道这是正确的吗?
public class ActivateDialogueTrigger : MonoBehaviour {
public GameObject DialogueTrigger;
public GameObject Enemy01;
public GameObject Enemy02;
public GameObject Enemy03;
public GameObject Enemy04;
public GameObject Enemy05;
public GameObject Enemy06;
void Start () {
DialogueTrigger.SetActive(false);
}
void Update () {
if(gameobjects.destroyed){
DialogueTrigger.SetActive(true);
}
}
}
答案 0 :(得分:1)
我看到两个选择:a"挑剔",但是干净的,和#34;脏"并且效率较低。
您可以在每个对象上附加一个脚本,负责在对象被销毁时调度事件。
public class OnDestroyDispatcher : MonoBehaviour
{
public event System.Action<GameObject> OnObjectDestroyed ;
private void OnDestroy()
{
if( OnObjectDestroyed != null ) OnObjectDestroyed( gameObject ) ;
}
}
然后,在你的脚本中:
public class ActivateDialogueTrigger : MonoBehaviour
{
// Drag & drop the objects in the inspector
public OnDestroyDispatcher[] OnDestroyDispatchers ;
// You will be able to add a function once all the objects are destroyed
public UnityEngine.Events.UnityEvent OnAllObjectsDestroyed;
void Start ()
{
for( int i = 0 ; i < OnDestroyDispatchers.Length ; ++i )
OnDestroyDispatchers[i].OnObjectDestroyed += OnObjectDestroyed ;
}
private void OnObjectDestroyed (GameObject destroyedObject)
{
CheckAllObjectsAreDestroyed();
}
private void CheckAllObjectsAreDestroyed ()
{
for( int i = 0 ; i < OnDestroyDispatchers.Length ; ++i )
{
if( OnDestroyDispatchers[i] != null || OnDestroyDispatchers[i].gameObject != null )
return ;
}
if( OnAllObjectsDestroyed != null )
OnAllObjectsDestroyed.Invoke() ;
}
}
销毁对象后,使用null进行等式检查将返回true。该对象实际上并不是真的,而是&#34; ==&#34;运算符被重载,以便您可以检查对象是否已被销毁。
public class ActivateDialogueTrigger : MonoBehaviour
{
// Drag & drop the objects in the inspector
public GammeObject[] YourGameObjects ;
// You will be able to add a function once all the objects are destroyed
public UnityEngine.Events.UnityEvent OnAllObjectsDestroyed;
void Update ()
{
CheckAllObjectsAreDestroyed();
}
private void CheckAllObjectsAreDestroyed ()
{
for( int i = 0 ; i < YourGameObjects.Length ; ++i )
{
if( YourGameObjects[i] != null )
return ;
}
if( OnAllObjectsDestroyed != null )
OnAllObjectsDestroyed.Invoke() ;
}
}