Network Instantiate
Reference Manual > Network Reference Guide > Network Instantiate

Network Instantiate

Network Instantiate lets you Instantiate a prefab on all clients in a very easy to use way. When a player calls Network.Instantiate(), they control any Input commands on that GameObject. All the setup and handling is performed under the hood, so all you need to do to assign control is make each player call Network.Instantiate() on the GameObject they should control.

Instantiating prefabs is one of the most important concepts in Unity. Network Instantiate is an extension to it, which lets you with a single function call instantiate an object across all machines on the network.

The argument list for Network.Instantiate() is as follows:

static function Instantiate (prefab : Object, position : Vector3, rotation : Quaternion, group : int) : Object

Like Instantiate the first three parameters describe the prefab to be instantiated and the position and rotation of where it should be placed. Additionally the group is passed in as an argument. Usually you just pass in 0 as the group. The group number allows you to group together network messages which allows you to filter them if so desired.

Technical Details

Behind the scenes, Network Instantiate is simply a convenience function built around RPC calls. It serializes an identifier of the prefab that is to be instantiated, serializes all allocated NetworkViewID's, serializes the position, and send it across the network using an RPC call. Network.Instantiate() will always buffer the Instantiate call in the RPC buffer, this also means it can be removed from the buffer in the same way as other RPC calls.

In essence this is a regular instantiation that is performed on all clients and automatically buffered. Players that join after the instantiation was performed will also receive the Instantiate call.

Communication Groups

Communication groups are important to differentiate communication and disable receiving/sending to some clients.

For example, two connected players might be in separate room without being able to meet. Thus there is no reason to transfer state of the two player avatars and depending on your situation. You could have one group for game data and one for chat communication. The game data group might be blocked when loading a level but the chat group kept open so players can communicate.