UnityEvent is not being heard by some objects
I have some projectile code subscribed to an event ("BossDied") and I can't figure out why they aren't firing the function.
Things I've tried:
I've used Debug.logs to check that they are being enabled and disabled properly. (Getting proper log messages when the objects are instantiated and destroyed)
I subscribed to the event from another script on a GameObject that
is instantiated at runtime. And here the dummy function I created is working.
public class XamoidProjectile : MonoBehaviour {
private void OnEnable() { EventManager.StartListening("BossDied", DestroyProjectile); Debug.Log("start listening" + this.name); } private void OnDisable() { EventManager.StopListening("BossDied", DestroyProjectile); Debug.Log("start listening" + this.name); } public void DestroyProjectile() { Debug.Log("DESTROOOYYYY!!"); }
public class BossMachine : MonoBehaviour {
public virtual void BossDied()
{
Debug.Log("Boss is dead");
EventManager.TriggerEvent("BossDied");
bossIsDead = true;
}
}
Answer by duckquark · Sep 28, 2018 at 05:37 PM
I found a solution by manually enabling the script through the main script controlling the projectile.
DestroyOnBossDeath tmp = GetComponent<DestroyOnBossDeath>();
tmp.enabled = true;
Now the code is being heard by the listener on the gameObjects that are Instantiated while the game is running.