I need to listen for an Event trigger but I can’t use the OnEnable() function because I am using ML Agents and the OnEnable() function on the Agent parent class is protected where else can I place the Event receiver if I can’t use OnEnable(), any suggestion would be welcome, thanks.
Maybe make a second script that has the OnEnable, attached to the same object and calls the method you want from this main script. You could either pass a reference of the main script to this one or us UnityEvent field and set it up in the inspector.
You can also just use the constructor. As long as you’re not accessing any Unity API or assigning values that will be overwritten by the serialization system it’ll work fine.
Uhm, because it IS protected you actually CAN override it. That’s the point of it being protected. It if were decared private you could not override it. Well you could reimplement it but that would of course hide the original. That’s why they declared most Unity callbace as virtual protected. Whenever you create a MonoBehaviour base class, this should be the default, otherwise a derived class can not implement those callbacks.
Don’t forget to call the base implementation, otherwise the class may not function as it should. This is actually mentioned in the comment at the OnEnabled method.
public class YourAgent : Agent
{
protected override void OnEnable()
{
base.OnEnable();
// additional OnEnable logic...
}
}