Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 761
Muhammad Faizan Khan Avatar asked Apr 22 '26 02:04

Muhammad Faizan Khan


2 Answers

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);
}
like image 179
shingo Avatar answered Apr 23 '26 16:04

shingo


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

like image 22
derHugo Avatar answered Apr 23 '26 14:04

derHugo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!