Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question UnityEvent passing parameters with the inspector

Discussion in 'Scripting' started by DonnyC1962, Mar 8, 2022.

  1. DonnyC1962

    DonnyC1962

    Joined:
    Feb 15, 2022
    Posts:
    19
    So I understand to pass parameters on a UnityEvent invoke(), I need to declare the event method like:

    Code (CSharp):
    1. public UnityEvent<string> SessionJoined;
    Great, then I can invoke the event like:

    Code (CSharp):
    1. SessionJoined.Invoke(_name);
    Now I want to hook this up using the inspector. How to I set the parameter? nothing comes through. Are parameters with the inspector all just hard codes?

    upload_2022-3-7_17-45-5.png
     
  2. DonnyC1962

    DonnyC1962

    Joined:
    Feb 15, 2022
    Posts:
    19
  3. MayankNexTechAR

    MayankNexTechAR

    Joined:
    Aug 7, 2022
    Posts:
    2
    Anyone found solution to this?
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    Well, there are two ways how you can hook up a method to a UnityEvent.
    • using dynamic parameter
    • using a single static parameter
    When using dynamic parameters you can even use UnityEvents with more than one argument as the arguments are passed by the invoker. When using static parameters the method can only take a single argument and that argument is serialized in the inspector. Such methods can be hooked even to parameterless events since the argument is provided in the inspector.

    Pay attention to the popup when you select your desired method:
    UnityEventDynamicParameter.png
    This is the example of a
    UnityEvent<string>
    .

    The red section are all the methods which actually take a string argument. This is the dynamic binding section. When you choose such a method the argument(s) passed by the invoker would be passed down to the actual method.

    The green section are any other methods that support static parameters. That includes methods without any parameter or exactly one parameter that can be serialized in the inspector. When you hook up one of those, you usually get the parameter input field in the inspector where you can specify the value that should be passed to the method. If such a method is bound to a
    UnityEvent<string>
    , it means the argument that the invoker passes to the event is ignored by this method since we pass a static argument.

    A UnityEvent only supports the following static types, since they need to be serialized inside the UnityEvent itself:
    • int
    • float
    • string
    • bool
    • UnityEngine.Object reference
    Those are the only argument types that can be serialized in the inspector of a UnityEvent. See the internal ArgumentCache class for more details.

    So pay attention to which version of your method you hook up.

    That's a red herring. A "delegate" implicitly contains a reference to the instance of a class. It comes with the same downsides, just that the reference is hidden in the delegate instance. Also C# events are really bad when it comes to frequent dynamic subscriptions or unsubscriptions. Also static events like shown in the link you posted are just global state on steroids. They are much more limited that way.

    The power / beauty of UnityEvents is that they can be hooked up in the inspector and this subscription can be serialized.

    ps: Note that there were a few Unity versions in between where they messed up the method selection. So in some cases there was no alternatives shown. Though this was fixed quite some time ago. So if you're on a more recent Unity version it should work as I shown in the screenshot.
     
    Last edited: Jul 26, 2023
    MohamedXIV, ow3n, look001 and 16 others like this.
  5. jefferson_rodrigues7

    jefferson_rodrigues7

    Joined:
    Jun 2, 2021
    Posts:
    2
    Thanks!