- Home /
How to fire UnityEvent with parameters passed programmatically
Hi! I've created my own selectable with an UnityEvent onValueChange. I programmatically invoke it with parameter but it seems that listeners receive parameter set in inspector rather than the one sent in Invoke call. Is there a way to set which argument should be passed? Or may I somehow get into that value from inspector to change it on the fly?
This is part of my setup:
Functions SetRightLaneCount(int count) are fired with count set to 0 or whatever is set in that field in inspector.
But I fire the event manually:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
using UnityEngine.Events;
public class IncrementalInput : Selectable
{
[SerializeField]
private Text text;
[SerializeField]
private Button PlusButton;
[SerializeField]
private Button MinusButton;
public int MinValue;
public int MaxValue;
private int currentValue = 1;
public int CurrentValue
{
get { return currentValue; }
set { currentValue = value;
if( CurrentValue == MaxValue )
PlusButton.interactable = false;
else if( CurrentValue == MinValue )
MinusButton.interactable = false;
else {
MinusButton.interactable = true;
PlusButton.interactable = true;
}
onValueChanged.Invoke(currentValue); //Event is fired
text.text = value.ToString();
}
}
[System.Serializable]
public class OnValueChanged : UnityEvent<int> { };
public OnValueChanged onValueChanged;
public void Add()
{
if( CurrentValue < MaxValue )
CurrentValue++;
}
public void Substract()
{
if( CurrentValue > MinValue )
CurrentValue--;
}
}
So everything works fine: currentValue changes, events are fired, but don't use correct variable as an argument.
Answer by Prosto_Lyubo · Mar 23, 2015 at 11:14 PM
Well, it was after all pretty simple. Functions cannot be static. If they have a form described in the Documentation there will be two functions on the list with the same name! One is in section with dynamic parameters functions and the other in section of static parameters functions. If You want to fire UnityEvent like the one with my code all You have to do is to choose the one from dynamic parameters section and everything will work perfectly. Event will be fired with parameters passed in the Invoke method.
Sorry to necro, but just to make it clearer what the difference is, here's an image: On the left is the "Dynamic" parameter and on the right is the "Static" parameter option!
Answer by Brijs · Jun 09, 2016 at 12:04 PM
Suppose you declared unityevent as follows
[System.Serializable]
public class ToggleButtonClickCallBack : UnityEvent<bool>{
}
public ToggleButtonClickCallBack OnToggle = new ButtonClickCallBack ();
Now OnToggle Unity Event will be shown in inspector with a bool parameter if you select static parameter method.
If you invoke OnToggle event with dynamic parameter it will only use parameter set in inspector and discard dynamic parameter that we passed at invocation time.
For that I have got a solution…..
When you want to call OnToggle event with dynamic parameter use Unityevent class methods as follows to call all the registered methods with dynamic parameter value as you want.
for(int i = 0 ; i < OnToggle.GetPersistentEventCount();i++ ){
((MonoBehaviour)OnToggle.GetPersistentTarget(i)).SendMessage(OnToggle.GetPersistentMethodName(i),dynamicValue);
}
Advantage of this is you can set a default value in inspector and call simply invoke to use default value and when you want dynamic parameters you can call as described above.
You can apply some filters also.
@Brijs do you know if this works nowadays? I'm doing something similar, but I get errors at runtime where Unity complains about the cast:
void SetValue<T>(UnityEvent e, T newVal)
{
string methodName = e.GetPersistent$$anonymous$$ethodName(0);
$$anonymous$$onoBehaviour mono = ($$anonymous$$onoBehaviour) e.GetPersistentTarget(0);
mono.Send$$anonymous$$essage(methodName, newVal);
}
@gamedev_fantasy yes, it still works - I've tested it in version 2021.2.5f1. I copied the original code snippet from the answer and had no issues. I realise this is an old thread, but wanted to answer your question just in case someone else wants an answer.
Thanks for the answer @Brijs! I can't believe this is what you have to do to invoke a unity event with a dynamic value!
Your answer
