0

I am working with a Unity game developer, and some part of his game requires us to pass the session data to the game in order to get the right user in the game. Unfortunately, every time I tried to pass the data using the docs he provided, I keep encounter the error "Failed to call function LoginSuccessful of class NetworkManager, Calling function LoginSuccessful with 1 parameter but the function requires 3.".

He gave me the function parameters and the data type I should follow, but somehow it still returns the same error. I would need the community guidance on this matter.

Unity Game Function

public void FunctionA ( int A, int B, string token ) {

}

My JS Script

var a = 1;
var b = 1;
var c = '8d4a7d1a-69cc-40f9-a74e-0fa7b388fbc6';
var script = document.createElement("script");
script.src = loaderUrl;
var MyGameInstance = null;
script.onload = () => {
  createUnityInstance(canvas, config, (progress) => {
    progressBarFull.style.width = 100 * progress + "%";
  }).then((unityInstance) => {
    MyGameInstance = unityInstance;
    loadingBar.style.display = "none";
    MyGameInstance.SendMessage('NetworkManager', 'LoginSuccessful', a, b, c);
    fullscreenButton.onclick = () => {
      unityInstance.SetFullscreen(1);
    };
  }).catch((message) => {
    alert(message);
  });
};

Error message when the game loads:

Failed to call function LoginSuccessful of class NetworkManager
Calling function LoginSuccessful with 1 parameter but the function requires 3.
cleopatez
  • 197
  • 1
  • 1
  • 17
  • `public void FunctionA ( int A, int B, string token )` I doubt the method is called this way ... it seems to rather be called `LoginSuccessful` ;) And then `SendMessage` supports onlyone single parameter .. why doing this at all? Seems like you could as well put your code directly within Unity in e.g. `Start` which is called anyway when the first scene finished loading – derHugo Jan 25 '23 at 07:21
  • @derHugo the situation is my company actually commissioned a unity game dev for a simple web game, so I just want to integrate the game to our current web app. Based on his docs, he did actually tell us to pass the session data to the game like that, with the 3 parameters using the SendMessage. So that's what boggles me since all the unity docs indirect that it should only be one parameter. I can't adjust any code within Unity since we only get the exported game instead of the code as well. – cleopatez Jan 25 '23 at 08:17
  • Then he/his docs are mistaken. Can't be done that way. Your Unity dev will have to change the c# side otherwise you can not work with it ^^ You will either stick to `SendMessage` with a single parameter (e.g. string + parse within c#) or you (actually he) will have to implement a JS-Plugin where you could in theory inject any c# callback with more parameters but that's something the Dev would need to do first as it is compiled into the webasm as well – derHugo Jan 25 '23 at 08:21
  • If they make you any trouble regarding that send them [this link](https://www.google.com/search?q=unity+JavaScript+sendMessage+multiple+paramters) -> there anyone can pretty much read and come to the same conclusion ;) – derHugo Jan 25 '23 at 08:27
  • This would basically be a duplicate of https://stackoverflow.com/questions/43654627/unity-5-6-error-when-calling-unity-function-from-webgl-sendmessage ;) – derHugo Jan 25 '23 at 08:31
  • Understood, then I guess I'll have to confront the dev regarding this issue, I thought there would be any workaround for this. But I guess it would be much simpler if it can be fixed from his side. Thank you for your comment :) @derHugo – cleopatez Jan 25 '23 at 09:48

1 Answers1

1

You cannot call that function via the MyGameInstance.SendMessage method.

From Unity docs:

Sometimes you need to send some data or notification to the Unity script from the browser’s JavaScript. The recommended way of doing it’s to call methods on GameObjects in your content. If you are making the call from a JavaScript plugin, embedded in your project, you can use the following code:

MyGameInstance.SendMessage(objectName, methodName, value);

Where objectName is the name of an object in your scene; methodName is the name of a method in the script, currently attached to that object; value can be a string, a number, or can be empty. For example:

MyGameInstance.SendMessage('MyGameObject', 'MyFunction'); MyGameInstance.SendMessage('MyGameObject', 'MyFunction', 5);

MyGameInstance.SendMessage('MyGameObject', 'MyFunction', 'MyString');

Reference: https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

To pass the data you want, FunctionA would have to be modified to only accept one parameter (for example a JSON string) that it would then parse to obtain the required parameters.

bosowski
  • 124
  • 6
  • Understood. I did have a read on the documentation as well, but I guess, this part would need the Unity Game Developer to modify his code, right? Do I have any other option other than requiring the developer to change his code? @bosowski – cleopatez Jan 25 '23 at 02:21
  • Yes, the method would have to be changed. You could change it yourself if you have access to the source code. – bosowski Jan 25 '23 at 02:28
  • Or a new method can be created that will take a string as argument, parse/process it and call the other method. – bosowski Jan 25 '23 at 02:38