EF inherits from E
SF inherits from S
S has attribute subE of type E
Runtime obj of type EF calls AddS(S arg) from its ancestor class (E) - passing runtime obj of type (SF)
Assignment attempt is in AddS: arg.subE = this
From attached image you can see that the runtime types match, but the assignment fails.
Key for image: E - Event; EF - EventFrameChange; S - Subscriber; SF - SubscriberFrameChange; AddS() - AddSubscriber; subE - subscribedEvent
Minimal, Complete, and Verifiable example
This code works in Unity. Create an empty GameObject in a scene and add a SpriteAnimatorScript as a component.
using UnityEngine;
public abstract class Event : ScriptableObject
{
public Subscriber[] subscribers;
public void AddSubscriber(Subscriber aSub)
//
{
aSub.subscribedEvent = this;
}
}
using UnityEngine;
public class EventFrameChange : Event
{
private SpriteAnimatorScript animatorScript;
public void Initialize()
// ^
{
subscribers = new Subscriber[0];
}
}
using UnityEngine;
public abstract class Subscriber : ScriptableObject
{
public Event subscribedEvent;
// ^ Event this is a subscription to.
public virtual void SetEvent(Event aEvent)
{
subscribedEvent = aEvent;
}
}
using UnityEngine;
public class SubscriberFrameChange : Subscriber
{
public new EventFrameChange subscribedEvent;
}
using UnityEngine;
public class SpriteAnimatorScript : MonoBehaviour
{
public EventFrameChange eventFrameChange;
public SubscriberFrameChange subscriberFrameChange;
void Start ()
{
SetupEvents();
}
private void SetupEvents()
{
eventFrameChange = ScriptableObject.CreateInstance<EventFrameChange>();
subscriberFrameChange = ScriptableObject.CreateInstance<SubscriberFrameChange>();
eventFrameChange.AddSubscriber(subscriberFrameChange);
}
}