0
\$\begingroup\$

I recently stumbled upon the UnityEngine.Events.UnityEvent class, and it seemed pretty cool so I started using it in a project.

Then I decided that I wanted to add some parameters to the event using its generic variants, like so:

[System.Serializable]
public class BarkEvent<GameObject> : UnityEvent<GameObject> {}

public class MyOtherClass : MonoBehavior {
    public BarkEvent<GameObject> bark;
    // ... Other stuff ...
}

But now it doesn't show up in the editor, making it harder to wire up my subscribers. Is there a way to make unity events with extra arguments show up in the Unity editor?

\$\endgroup\$
  • 2
    \$\begingroup\$ Try changing BarkEvent<GameObject> to just BarkEvent in both instances. I can't really try this out but if this works, please try and turn it into an answer :) \$\endgroup\$ – John Hamilton Apr 13 '17 at 5:59
1
\$\begingroup\$

@JohnHamilton is right. Because Unity can't serialize generic types in the inspector (not easily, workarounds exist I think), your event won't be displayed. The purpose of inheriting from the UnityEvent type is to get rid of the generic type and be able to serialize it in order to be shown in the inspector.

Your event is "designed" to handle GameObjects only, thus, you don't have (and should not) specify the GameObject type between the chevrons.

[System.Serializable]
public class BarkEvent : UnityEvent<GameObject> {}

public class MyOtherClass : MonoBehavior {
    public BarkEvent bark;

    public void YourFunction()
    {
         if( bark != null )
             brak.Invoke( gameobject ) ;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.