- Home /
Send Prefab by RPC?
How can I send a prefab by RPC?
Right now, I am sending prefab's as strings, and calling Resource.Load() on everyone that receives the RPC, but this is very inconvenient, because it forces me to keep all my prefabs in the resource folder, and also to manually type out the names of all the prefabs.
In the documentation for Network.Instantiate, it says that unity uses an "identifier" for the prefab and sends an RPC. Where exactly does this identifier come from?
I would very much like to see this question answered. Unity's networking is severely lacking. The least they could do is expose enough internals to let us fix their mistakes.
Answer by syclamoth · Feb 19, 2012 at 01:24 AM
Well, since presumably both computers already have the prefab (because it was packaged with the game), all you need to do is send a 'signal' RPC which includes a 'NetworkViewID'- this will allow you to synchronise two objects, assuming that they are compatible.
When the signal RPC arrives, the receiver should spawn the correct prefab, and the assign the received NetworkViewID to the newly created object's networkView.viewID field.
So, all you need to do is assign the prefabs in the inspector, like normal, and then just make sure that they have networkViews, and that the instantiation happens in an RPC call that knows what prefab to spawn. Remember that unless you change the references at some point (which is always going to be a problem for prefab instantiations), it will just work.
That won't really work in my case. Basically, what I am trying to do, is create a custom Network.Instantiate that sends a reference to the NetworkPlayer that Instantiated the prefab along in the same RPC. The unity docs say that I should be able to get that information in OnNetworkInstantiate() from either info.sender or networkView.owner, but the data in those two variables is very inconsistent. In many cases, they can contain -1(empty), or 0(the server) even when it was a client that initially called for the instantiation... So how am I supposed to know who instantiated something?
this post also talks about the issue above: http://forum.unity3d.com/threads/51312-networkView.owner-issue
Ah, I've had this exact problem before. In the end what I had to do was create (when a player joins) a 'Player' object that was definitely owned by the newly spawned player. Then, I could use references to it, ins$$anonymous$$d of relying on networkView.owner etc.
yea, that still left me with the problem of manually sending the rpcs so I could send that reference along. I ended up just making a persistent GameObject with a list of all the prefabs that, which sets an index in each prefab's base class when the game starts, which allows me to just reference the prefabs by an integer index. Definitely not ideal, but it does the trick.