PUN: Sending GameObjects Over the Network

One of the biggest challenges I faced when I started learning to network in Unity was figuring out how to access a GameObject via RPC .  Since Unity’s networking package and Photon Unity Networking only support a strict list of data types as parameters for RPCs (as seen here), I needed to find a way to access GameObjects and components over the network.

The solution to this is by using the viewIDs associated with the NetworkViews/PhotonViews of these GameObjects.  For example, in Space Dunk, when the player picks up or drops a ball, they send their GameObject around, so other clients know to child the ball to that player GameObject’s socket.

So first, I get the PhotonView attached to the player GameObject, in this case: playerToDrop

The RPC will send the viewID of the player’s PhotonView. The viewIDs are integers that are unique to that exact GameObject, and are the same across all clients.
Passing that viewID to PhotonView.Find() will return the PhotonView associated with that ID. From there, you can retrieve the GameObject easily.

 

I originally couldn’t find anything online about this, and I had to do a lot of digging through Photon’s documentation to figure it out. It’s one of the most useful tricks I’ve learned.

Leave a comment