Passing a params array into RPC call

I have a RPC function defined as follow :

[RPC]
void TestRPC(NetworkViewID a_viewID, Vector3 a_f3Point, params object[] args)

I would like to pass an unknown number of parameters, after the vector3, of undefined type (but still in the list of available parameter type for RPC call).

I tried to call the RPC as follow :

object[] args = new object[]{45.0f};
networkView.RPC("TestRPC", RPCMode.Server, viewID, hit.point, args);

When the RPC is called with 1 float in the object args, args.Length is equal to 1 but when I try to get it, it fails : “NullReferenceException: Object reference not set to an instance of an object”

Debug.Log(args.Length) => 1
Debug.Log(args[0]) => FAILURE

I also tried to call it that way :

networkView.RPC("TestRPC", RPCMode.Server, viewID, hit.point, 45.0f);

but it said that the RPC function doesn’t match the parameters.

Is there a way to do that? Is there any workaround?

You can get around passing a variable number of parameters by:

  • Adding your object parameters to an array ensuring that they are all [Serialzable] or POCO (Plain Old CLR Object). You cannot pass references to UnityEngine.Object derivatives this way
  • Use BinaryFormatter to save the object array to a byte
  • Pass the byte by RPC
  • Dehydrate the array and use the values.

I built an RPC extender that does this for you which you can get from http://whydoidoit.com/2012/06/29/inheritable-rpc-calls-for-unity/

The solution is simple: you must declare every variable you want send explicitely in one of the supported types.
Unity does not support arrays for serialization, only single variables of the types listed at http://docs.unity3d.com/Documentation/Components/net-RPCDetails.html

The only workaround to send a flexible amount of data is writing an own byte > string serialization and sending string.

The alternative is to use a Unity Networking alike networking which is capable of proper, modern data type support like Photon Unity Networking (PUN) or MuchDifferents uLink (part of the UnityPark Suite)

I think your capitalization is wrong.

Instead of object I think you should have Object.
object is a system struct while Object is the unity one.

I have similar code and it works fine.