# SerializedProperty to UnityEvent in a Custom Editor

I am making a Custom Editor for a type that includes a UnityEvent property.

I'm currently facing an issue in converting a SerializedProperty to get that UnityEvent.

Here is how I declare the event in the type I want to edit with this custom editor:

public class SomeComponent : MonoBehaviour {

//...
}


And this is my Editor script

[CustomEditor(typeof(SomeComponent))]
public SomeComponentEditor : Editor {

private SerializedProperty _Events;
private void OnEnable()
{
}

public override void OnInspectorGUI()
{
TwitchTVGui TGUI = (TwitchTVGui)target;

this.serializedObject.Update();

this.serializedObject.ApplyModifiedProperties();
}
}


And then I need to get the UnityEvent out of the serializedObject _Events somehow

• Have you tried serializedObject.FindProperty("EventToAdd").objectReferenceValue as UnityEvent? Feb 19 '19 at 12:14

Huh, I literally just had this problem and found this code over here, but it basically boils down to needing just this:

[Serializable] public class CustomEvent : UnityEvent { }

public CustomEvent myEvent = new CustomEvent();


Rename the class and field as you see fit. If you're going to use the same event in multiple script files, make it its own script, but having a micro-class for each one is fine too (it distinguishes that OnWidgetDoX and OnWidgetDoY are different events).

If I understand correctly, you are trying to serialize events so that they will show up in the inspector. You do not need to do that yourself.

Instead, just use UnityEngine.Events.UnityEvent, and it will be available in the inspector for you. This is simply a wrapper around standard C#.

public UnityEvent MyEvent;

• First of all thank you for your answer.I am currently working on a custom editor for our game designer and for that I am using serializedProperties. I can access strings,ints & co , but not UnityEvent... Jan 15 '19 at 23:35
• @US3L3SS if you add the above line of code, it will show up in the inspector. Is that what you are trying to do?
– Evorlor
Jan 15 '19 at 23:49
• No I want to get the UnityEvent proberty... Jan 16 '19 at 0:21