Hi,
I am trying to set an UnityEvent to invoke a function with a parameter of type string, which is in a script attached to another game object, but the value of the string that reaches the invoked function OnPlayerName is null:
(I am using unity 2021.3.24f1 on ubuntu 22.04)
//Attached to GameObject: UI
public class StartScreen : MonoBehaviour {
public UnityEvent<string> playerName;
private void Start() {
playerName?.Invoke("aName");
}
}
//Attached to GameObject GameManager
public class SetupPlayer : MonoBehaviour {
public void OnPlayerName(string s) {
Debug.Log(s); //s="" rather than "aName"
}
}
In the inspector the UnityEvent StartScreen.playerName is initialized to the object GameManager script SetupPlayer.OnPlayerName(string s).
OnPlayerName function is called but the value of the string is lost, s is empty s=“” rather than s=“aName”.
However moving the OnPlayerName(string s) function to the same class StartScreen it gets the correct value.
//Attached to GameObject: UI
public class StartScreen : MonoBehaviour {
public UnityEvent<string> playerName;
private void Start() {
playerName?.Invoke("aName");
}
public void OnPlayerName(string s) {
Debug.Log(s); // s = "aName"
}
}
It is NOT possible to pass parameter values using UnityEvents from one game objects to another?
I did some debugging.
If the invoker and the invoked are in the same class, the string argument is passed fine, because it invokes using lib function:
internal class InvokableCall<T1> : BaseInvokableCall
//...
Invoke(T1 args0){
if (!BaseInvokableCall.AllowInvoke((System.Delegate) this.Delegate))
return;
this.Delegate(args0);
}
}
But if they are in different game objects it uses another lib function to invoke, which overrides the one above, and does not pass the right arg0 argument but this.m_Arg1:
internal class CachedInvokableCall<T> : InvokableCall<T> {
//...
public override void Invoke(T arg0) => base.Invoke(this.m_Arg1);
}
Note: The same happens when the OnEndEdit event of an InputField is set to the SetupPlayer.OnPlayerName(string s) method. The value of the text in the input field does not reaches the event function OnPlayerName(string s), whether it is attached to the InputField or to another game object.




