How to assign object and function in inspector like Button's OnClick()?

Hello,

I want to call some object’s some function which can be assigned in inspector in my C# script.
It’s perfect if I can assign object and function just like Button UI’s OnClick stuff.

How can I do that? Any idea will be appreciated.

Add a UnityEvent property to your script.

public class ExampleClass : MonoBehaviour
{
    public UnityEvent myEvent;
  
    void Start()
    {
        myEvent.Invoke();
    }
}

If you want to support an argument then do this: Unity - Scripting API: UnityEvent<T0>

37 Likes

Wow! That’s exactly what I wanted. Thank you very much!

1 Like

I was searching for this simple thing for far too long. Thank you.

3 Likes

That was exactly what i was looking for.

nice thanks man :slight_smile:

thank you

This is so useful

Can you please tell me how to launch this type of function in unity script