Search code examples
unity-game-engineunity-webgl

SendMessage with array paramter in unity webgl


Unity SendMessage can only pass one parameter and it can be an array. So i am calling my sendMessage for javascript and calling C# method(acutally webgl method)

var arr = [x,y,z];
gameInstance.SendMessage("Cube","SetGameObjectPosition",arr);

but getting this error

Invoking error handler due to

Uncaught 2,2,2 is does not have a type which is supported by SendMessage. [Violation] 'click' handler took 8994ms blob:http://localhost/1ff50200-cb3a-4367-ab45-f02e9734fac2:2 Uncaught 2,2,2 is does not have a type which is supported by SendMessage.

SendMessage @ blob:http://localhost/1ff50200-cb3a-4367-ab45-f02e9734fac2:2

SendMessage @ UnityLoader.js:4

SetObjectPosition @ (index):44

onclick @ (index):65 (index):65

[Violation] 'click' handler took 9000ms


Solution

  • From the SendMessage Docu

    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.

    -> no it can not be an array


    But it seems you want to pass on a position so as a workaround you could pass in the string like "2,2,2" and use

    string[] numberStrings = ("2,2,2").Split(",");
    float x = float.TryParse(numberStrings[0], out x) ? x : 0;
    float y = float.TryParse(numberStrings[1], out y) ? y : 0;
    float z = float.TryParse(numberStrings[2], out z) ? z : 0;
    

    or something like that