Reputation: 610
SendMessage call:
gameInstance.SendMessage("MessageReceiver", "Test", "This is a message", "myname");
Error message:
Failed to call function Test of class MessageReceiver
Calling function Test with 1 parameter but the function requires 2.
(Filename: Line: 810)
And the function definition:
public void Test(string message, string name) {
// If the call from JavaScript succeeds, an alert will show up on the page
Application.ExternalEval("alert('it works')");
}
The first parameter, "MessageReceiver", is the unity game object the script is attached to.
The second one, "Test", is the name of the function being called.
The rest of the parameters are passed to the called function, in my case, "Test".
As you can see, I'm passing 2 string parameters, which is exactly what "Test" receives. Why then is an error telling me that I'm calling the function with 1 parameter? Any help would be appreciated.
Upvotes: 0
Views: 3225
Reputation: 415
You can't do this with the current version of Unity (v2018.4.8f1) nor with any previous versions.
You can check what gameInstance.sendMessage
does:
It actually just calls gameInstance.Module.SendMessage
:
function ƒ() {
if (a.Module.SendMessage)
return a.Module.SendMessage.apply(a.Module, arguments)
}
Which ignores multiple params:
function SendMessage(gameObject, func, param) {
if (param === undefined) Module.ccall("SendMessage", null, ["string", "string"], [gameObject, func]);
else if (typeof param === "string") Module.ccall("SendMessageString", null, ["string", "string", "string"], [gameObject, func, param]);
else if (typeof param === "number") Module.ccall("SendMessageFloat", null, ["string", "string", "number"], [gameObject, func, param]);
else throw "" + param + " is does not have a type which is supported by SendMessage."
}
Usually Unity WebGl2 devs do use multiple methods at the C# side. However in some cases its not possible to use multiple methods at the C# side so they stick using a string for multiple params (because as you can see only numbers and strings are supported at JS).
You can send multiple params from C# to JS.
You can send only one param from JS to C#.
In both cases you can only send strings or numbers (int, float, double).
Upvotes: 1