Before updating to Windows 10 you may need to return your Pro license. More information here

Problem sending array to unity player using SendMessage


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?

more ▼

asked Feb 15, 2011 at 01:46 AM

avatar image

1 3 2 2

Perhaps divide the data?

Feb 15, 2011 at 01:48 AM fireDude67

Even dividing the data when I have more that 400,000 items in each array will cause performance issue.

Feb 15, 2011 at 02:01 AM Aboo
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.


Getting another script, where otherscript is the name of the script you are sending the stuff to.

OtherScript other = FindObjectOfType(typeof(OtherScript));

If other script is attached to same object can just do.


Or if you want to get it off a specific gameobject

var go = GameObject.Find("SomeGuy");

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.

more ▼

answered Feb 15, 2011 at 02:49 AM

avatar image

1.2k 45 39 56

Thanks for your reply.

Please note that I am calling the SendMessage 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 SendMessage 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.

Feb 15, 2011 at 03:19 AM Aboo

edited an example of finding another script.

Feb 15, 2011 at 03:50 AM tertle

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.

Feb 15, 2011 at 08:27 AM Aboo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Feb 15, 2011 at 01:46 AM

Seen: 3309 times

Last Updated: Oct 26, 2014 at 05:59 PM