Question about UnityEvents

I have been messing around with UnityEvents and really like them! I’ve been looking at a lot of different tutorials and noticed that there seems to be two ways to use them and I’m just curious if there’s a functional difference between the two that I should be aware of. I hope the terms I use make sense… I’m still wrapping my head around that as well.

Method 1 - Serialize at the top, then create variables with a type that matches the serialized definition followed by the name of the event.

[System.Serializable] public class CheckForWin : UnityEvent<int, int, int> { }
...
public UIScoreUpdate OnUpdateUIScore;

Method 2 - Serialize at the top, then create variables of type UnityEvent and include the parameters if present, followed by the name of the event.

[System.Serializable] public class CheckForWin : UnityEvent<int, int, int> { }
...
public UnityEvent<int, int, int> OnUpdateUIScore;

The second method looks a whole lot cleaner and is far more intuitive to me but I’m curious if I’m setting myself up for frustrations later on with that approach.

The former method is required in somewhat older versions of Unity (I’m thinking around < 2019 versions) because there was no support for generic serialization.
If you wanted a UnityEvent with parameters to show up in the inspector, you had to make a dedicated non-generic class that inherits from a generic UnityEvent type, and create a field of that inherited type instead.

Newer versions of Unity do support generic serialization though, and the latter method is perfectly fine to use with them.

1 Like