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
You can pass a json string:
var pos = {x:1,y:2,z:3};
gameInstance.SendMessage("Cube","SetGameObjectPosition", JSON.stringify(pos));
In unity:
void SetGameObjectPosition(string data)
{
var position = JsonUtility.FromJson<Vector3>(data);
}
From the SendMessage Docu
SendMessage(objectName, methodName, value);Where
objectNameis the name of an object in your scene;methodNameis the name of a method in the script, currently attached to that object;valuecan 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With