Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot implicitly convert type `UnityEngine.Events.UnityAction<string>' to `UnityEngine.Events.UnityAction'

Tags:

c#

delegates

I want to send arguments through the standard Unity listener, as per the tutorial.

mbListener = new UnityAction<string>(SomeFunction);

void SomeFunction(string _message)
{
    Debug.Log ("Some Function was called!");
}

Why is this failing with the above error message? BTW I am looking for practical answers and really don't care for tech-talk.

(NB Unity's own manual says it can handle arguments but I cannot work out why this is wrong).

like image 960
Philip Avatar asked Sep 01 '25 20:09

Philip


1 Answers

What did you declare mbListener as? Probably its of type - UnityAction. Declaring it as UnityAction and assigning it with UnityAction<string> is causing you the problem.

Based on your requirement, You can do either of these 2 to fix -

UnityAction<string> mbListener = new UnityAction<string>(SomeFunction);

or

UnityAction mbListener = new UnityAction(SomeFunction);
void SomeFunction()
{
    Debug.Log ("Some Function was called!");
}

Edit As @MotoSV pointed out... you should call it bymbListener("String parameter");

mbListener is a place holder for any function/listner you wanted to call. When you need it to be called you just have to call the UnityAction variable passing the parameter to it. So mbListener("String parameter"); will work for you.

like image 128
Carbine Avatar answered Sep 03 '25 13:09

Carbine