Hey all, after updating to Unity2020 I’ve ran into a bit of a curious issues with my scriptableobject event system where I can’t assign my objects to inspector if the object is generic
First of here is my “main” code and base classes
[CreateAssetMenu( menuName = "Events/GameEvent" )]
public class GameEvent : ScriptableObject, IGameEvent
{
private List<IGameEventListener> _listeners = new List<IGameEventListener>();
[Button]
public void Invoke()
{
for (int i = _listeners.Count - 1; i >= 0; i--)
{
_listeners[i].OnEventRaised();
}
}
public void RegisterListener(IGameEventListener listener)
{
_listeners.Add(listener);
}
public void UnregisterListener(IGameEventListener listener)
{
_listeners.Remove(listener);
}
}
public abstract class GameEvent<T> : GameEvent, IGameEvent<T>
{
private List<IGameEventListener<T>> _listeners = new List<IGameEventListener<T>>();
public void Invoke(T value)
{
for (int i = _listeners.Count - 1; i >= 0; i--)
{
_listeners[i].OnEventRaised(value);
}
}
public void RegisterListener(IGameEventListener<T> listener)
{
_listeners.Add(listener);
}
public void UnregisterListener(IGameEventListener<T> listener)
{
_listeners.Remove(listener);
}
}
Next I’ve inherited the generic game event like this so I can create specialized events with the specific type I wanna parse in the event:
[CreateAssetMenu(menuName = "Events/BoolGameEvent")]
public class BoolGameEvent : GameEvent<bool>
{
}
Now in a different script I’d add this so I can call .Invoke(), Please note this is not the full script but only the related part of it and there is NO errors on my projects:
public class PowerUp : MonoBehaviour
{
[SerializeField]
GameEvent _event;
public void ExecutePowerUp()
{
if (_event != null)
{
if (_event is GameEvent<int> gei)
{
gei.Invoke(_targetEventValue);
}
else if (_event is GameEvent<bool> geb)
{
geb.Invoke(true);
}
else
{
_event.Invoke();
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
ExecutePowerUp();
Destroy(gameObject);
}
}
}
With all my code out of the way I now add my Powerup script to a gameobject:
I’ve now also created a Scriptableobject of BoolGameEvent as an asset;
But now if I open the the object picker from my powerupscript this event is nowhere to be found:
And if I try to drag and drop the script on to the script I get this error:
All this seems to work fine in unity 2019.4(LTS) but stopped working in 2020
I’ve already tried the following to resolve my problem without luck:
Restart unity and VS
Reimport All
Delete the library folder
Rename The Generic GameEvent class
Put the Generic GameEvent class into its own file
Rename the generic GameEvent and put it into its own file
Recreate the ScriptableObject asset in 2020
Change the type of _event variable in my PowerUp Script to GameEvent
Changed GameEvent to inherit from ScriptableObject instead of GameEvent (and change related code to work with this change)
Changed SerializeField to SerializeReference (not sure if this is related to generic objects or only collections so had to try)
What seems to work is:
Open Unity 2019.4 and assign the GameEvents from there seems to work and be recognized in the 2020 version, but I’m still unable to change the GameEvent
Does anyone know what the cause of this could be and how I can fix it?
- Thanks a lot in regard!