- Home /
Sending a System.object over RPC
So I've discovered that you can't cast something as an object and send it as a parameter through RPC. You can't do this because, your object will be instantly recasted as it's original type.
For instance if I make an RPC method that takes an "object" as it's parameter. If I cast a string as an object and call the RPC method using that as one of the arguments, it will throw an error saying something like "Photonview has no method "MethodName" that takes 1 argument(s): string"
Is there any way to bypass this and just send a generic object? It would make way more sense than me needing to make a new method for each type.
Note that I'm using Photon's PUN+, I don't know if this is still(was) the case with Unity's built-in RPC handler
Why do you cast your string when calling the RPC since System.String directly inherits from System.object ? Though, you will have to cast your object into a string inside the body of your RPC.
That was just an example. I'm trying to send hashtable values through RPC which are by default system.object's. The values aren't limited to any one type either. Some are strings, ints, floats, byte[]s and custom objects as well. It just seems silly to make a new function for each one of these types.
Take a look at byte[] serialization with BinaryFormatter :
BinaryFormatter $$anonymous$$SDN :
StackOverflow threads
http://stackoverflow.com/questions/15750154/serialize-an-object-in-c-sharp-and-get-byte-stream
Then, make your RPC function take a byte array as parameter
You cannot directly send an Object, Prefab, GameObject etc. over RPC but you can serialize it, send the serialized data and deserialize it when received.
I've thought about that, I was just hoping there was an easier way to force an RPC to take the object as a parameter without changing it. But if there's not, then I guess that's what I'll end up doing.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201306im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Error after delete object! 0 Answers
How do I setup the Unity Network Connection Facilitator 0 Answers
Object reference not set to... 1 Answer
NullReferenceException 0 Answers
Using entities and jobs 1 Answer