0

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.

derHugo
  • 83,094
  • 9
  • 75
  • 115
SoKeT
  • 590
  • 1
  • 7
  • 16
  • What is `gameInstance`? – Programmer Apr 27 '17 at 10:07
  • @Programmer It's necessary to make the game run on the web: `var gameInstance = UnityLoader.instantiate("gameContainer", "Build/WebGL.json");` – SoKeT Apr 27 '17 at 10:11
  • Looks like a 5.6 release feature. What happens when you use `'` instead of `"`?. For example `gameInstance.SendMessage('MessageReceiver', 'Test', 'This is a message', 'myname');`. Not really sure that's helpful but also try to add one more parameter and see what happens. Try to call the function without parameter at-all. Do the experiment and tell us where it breaks. Maybe, it even fail without parameter at-all.... – Programmer Apr 27 '17 at 11:17
  • @Programmer I made it work yesterday on Chrome's console with `"`, so that shouldn't be the problem. The call works well when the receiving function has one or no parameters, but it fails if it has 2 or more, even if you pass more parameters on the call. – SoKeT Apr 27 '17 at 12:59

1 Answers1

1

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).

RajmondX
  • 405
  • 2
  • 9