Note: UNet is deprecated, and will be removed from Unity in the future. A new system is under development. For more information and next steps see this blog post. |
There are two types of networked GameObjects in Unity’s multiplayer system:
Those that are created dynamically at runtime
Those that are saved as part of a Scene
GameObjects that are created dynamically at runtime use the multiplayer Spawning system, and the prefabsAn asset type that allows you to store a GameObject complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. More info
See in Glossary they are instantiated from must be registered in the Network ManagerA Networking component that manages the network state of a Project. More info
See in Glossary’s list of networked GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary prefabs.
However, networked GameObjects that you save as part of a SceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary (and therefore already exist in the Scene when it is loaded) are handled differently. These GameObjects are loaded as part of the Scene on both the client and server, and exist at runtime before any spawn messages are sent by the multiplayer system.
When the Scene is loaded, all networked GameObjects in the Scene are disabled** **on both the client and the server. Then, when the Scene is fully loaded, the Network Manager automatically processes the Scene’s networked GameObjects, registering them all (and therefore causing them to be synchronized across clients), and enabling them, as if they were spawned at runtime.
Saving networked GameObjects in your Scene (rather than dynamically spawning them after the scene has loaded) has some benefits:
When the Network Manager spawns the networked Scene GameObjects, those GameObjects behave like dynamically spawned GameObjects. Unity sends them updates and ClientRPC calls.
If a Scene GameObject is destroyed on the server before a client joins the game, then it is never enabled on new clients that join.
When a client connects, the client is sent an ObjectSpawnScene spawn message for each of the Scene GameObjects that exist on the server, that are visible to that client. This message causes the GameObject on the client to be enabled, and has the latest state of that GameObject from the server in it. This means that only GameObjects that are visible to the client, and not destroyed on the server, are activated on the client. Like regular non-Scene GameObjects, these Scene GameObjects are started with the latest state when the client joins the game.