Assign UnityEvent in script

Hey guys/gals,
When you declare a public UnityEvent you can set it in the inspector, as we know. I’m wondering if you can do this in script. I made an editor script for my team that instantiates objects for a scene in our project but they need to manually set the UnityEvents for a script on those objects, and I’m trying to automate that. Much appreciated.

Like at runtime register an event handler? Use the AddListener method:

Or, do you mean attach a listener via an editor script that then shows up in the inspector? If so, you’ll need to use the UnityEventTools (they’re in UnityEditor.Events and are editor time only):

3 Likes

I’m looking for a script equivalent of setting the event in the inspector like you’d normally do.

6948887--817355--Capture.JPG

Use UnityEventTools… it’s the second paragraph in my post.

Note, UnityEventTools is EDITOR-ONLY. You can not deploy that in runtime (nor would you want/need to… there’s no inspector in a deployed runtime. You use the AddListener for that.).

1 Like

Okay, I just didn’t understand what those methods did. Thanks a lot duct!