Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting UnityEvents from the editor and making them call the right argument

Tags:

unity3d

I am working with UnityEvents as stated here. There it is written

When configuring a UnityEvent in the Inspector there are two types of function calls that are supported:

Static. Static calls are preconfigured calls, with preconfigured values that are set in the UI. This means that when the callback is invoked, the target function is invoked with the argument that has been entered into the UI.

Dynamic. Dynamic calls are invoked using an argument that is sent from code, and this is bound to the type of UnityEvent that is being invoked. The UI filters the callbacks and only shows the dynamic calls that are valid for the UnityEvent.

so I get a UnityEvent with an argument as in

 public class UnityEventFloat : UnityEvent<float>
    {
    }
public UnityEventFloat OnUpdateEvent;

This appears in the inspector as

A callback entered to a UnityEvent

As you can see I have added a callback function setWaitTime to the UnityEvent. The function has this shape public void setWaitTime(float t)

After that the UnityEvent is going to be invoked in code as

OnUpdateEvent.Invoke(aValue);

Trying all this, it is not working well since the function when invoked is using as argument 0 (as in the Inspector) What I want is that it calls the function with the argument aValue (which is an internal variable in code)

How can I do this? I guess I am not quite understanding the quote above from the documentation. My situation is like the "Static" one, and I require it works like the "Dynamic"


EDIT2:

I got to solve the problem, so I am going to write it in an answer


EDIT:(Old) I tried the proposed answer and I got this enter image description here I don't know how this can solve the problem (notice that now the parameter is the script. But this is inside the same script (SliderGestureControl))

like image 256
KansaiRobot Avatar asked Jun 29 '18 05:06

KansaiRobot


People also ask

What is action in unity?

Actions in Unity Actions are, essentially, ready-made delegates. They can be used to easily create a delegate or delegate event with a void return type. Which means that you can use an action in the same way as you would a delegate, the difference being that you don't need to declare it first.


1 Answers

I finally got to make it work. Apparently I was making a mistake when selecting the function from the drop down list. I selected the function under the "Static" label but I should have selected the same function but under the "Dynamic" label that was at the top of the list (and I didn't see).

With that the function setWaitTime does not have any parameter in the inspector (so it uses dynamically what is given to it by the UnityEvent.

like image 150
KansaiRobot Avatar answered Sep 26 '22 02:09

KansaiRobot