- Home /
Question by
brusrecenthusky · Apr 27 at 11:16 AM ·
c#eventsanswers
ScriptableObject with C# event not working in unity editor, but working in build, is there an explanation as to why?
Unity version is 2021.3.0f1 LTS. I have a Scriptable Object that contains a c# event and a couple methods to add, remove, and clear listeners. Yesterday, the project worked as intended in the unity editor where if the c# event was invoked, the listeners would then execute, but now, whenever I try to execute it, nothing happens. So I tried creating a build to see if it would work there, and it does. Any explanation to this? Are c# events not very compatible with scriptable objects? This is my code:
public class RSEvent : ScriptableObject
{
#region Simple
private event Action _simpleEvent;
public void Invoke()
{
_simpleEvent?.Invoke();
}
public void AddListener(params Action[] listeners)
{
foreach (Action listener in listeners)
{
_simpleEvent += listener;
}
}
public void RemoveListener(params Action[] listeners)
{
foreach (Action listener in listeners)
{
_simpleEvent -= listener;
}
}
#endregion
#region Complex
private event Action<object> _complexEvent;
public void Invoke(object args)
{
_complexEvent?.Invoke(args);
}
public void AddListener(params Action<object>[] listeners)
{
foreach (Action<object> listener in listeners)
{
_complexEvent += listener;
}
}
public void RemoveListener(params Action<object>[] listeners)
{
foreach (Action<object> listener in listeners)
{
_complexEvent -= listener;
}
}
#endregion
}
Comment