- Home /
Problem sending array to unity player using SendMessage
Hi,
I am trying to call the SendMessage function in order to call a method within unity player from web browser and I want to pass an array of integers or array of bytes. It works only if the length of the array is less than 1024. When the length of the array goes more than 1024 then I receive :
Error calling method on NPObject!
A sample code is like this:
var a = new Array();
var i = 0;
for (i = 0; i < 1025; i++) {
a[i] = i;
}
unity.SendMessage("MyObject", "MyMethod", a);
The method within unity player is like this:
void MyMethod(Array array)
{
Debug.Log("received array length : " + array.Length);
}
As I mentioned everything works as expected until the length of the array will be more than 1024. However if I pass a string it could be very long. I tested a string with length 1,000,000 and it worked. Unfortunately in my case I need the array and converting the string back to array will significantly affect the performance.
Any idea?
Even dividing the data when I have more that 400,000 items in each array will cause performance issue.
Answer by tertle · Feb 15, 2011 at 02:49 AM
I was going to write this as a comment but got to long.
The fact it's the magic number of 1024 I'm going to assume it's just a engine problem with either SendMessage or Array.
So how many components are you sending this to? Seems kind of poorly designed/inefficient if you ask me if you need to send such a large amount of data to a large number of places. If it's just a single other script you should really just get a direct reference to it.
Also out of interest, if you encapsulate the array it in another class and send the class does it still cause the same issue?
Finally, have you considered trying an ArrayList instead? Or just a basic native .NET arrays if you don't need to resize it (assuming above code isn't what is in your game.) I don't really like the built in Unity Array.
-edit-
Getting another script, where otherscript is the name of the script you are sending the stuff to.
OtherScript other = FindObjectOfType(typeof(OtherScript));
other.MyMethod(a);
If other script is attached to same object can just do.
GetComponent(OtherScript).MyMethod(a);
Or if you want to get it off a specific gameobject
var go = GameObject.Find("SomeGuy");
go.GetComponent(OtherScript).MyMethod(a);
You shouldn't do a Find every update for performance reasons, but you can do it once in Start() and just store it and call it anytime you want.
Thanks for your reply.
Please note that I am calling the Send$$anonymous$$essage from javascript within client browser. So I don't have a .Net array or ArrayList in client side javascript.
The problem is even before reaching unity player and at the script level. It looks like that the Send$$anonymous$$essage method in client side can not handle arrays of length more than 1024.
I am calling only one component and I don't know what do you mean by getting a direct reference.
I appreciate your time but this looks like a code written to be run inside unity player. I have no problem at this stage. What I am doing is trying to call the method via web browser Javascript.