SNetworkObject
All object prefabs that need to be spawned or despawned must have the SNetworkComponent attached. This component is required if you inherit any other scripts from SNetworkBehaviour on the same object.
Spawn()
Starts syncing the object with clients. If the object does not exist on the client it will be instantiated. This function must be called on the server side. You may pass the owner client_id as parameter, by default the owner is the server.
Make sure you registered the prefab with TheNetwork.Get(). RegisterPrefab() before trying to spawn so that all clients are aware this prefab exists.
Note that you do not need to call Spawn() manually if auto_spawn is active on the component.
Despawn()
Stops syncing the object with clients. The object won't be destroyed and will stay in the scene. Must be called from the server side.
Destroy()
Despawns and then destroys the object on all clients and the server. It is recommended to NOT use the default unity GameObject.Destroy() function for any object with SNetworkObject component, and use this one instead. To make sure that the object gets destroyed everywhere instead of just locally.
ChangeOwner(ulong)
Change the owner of the object. Must be called on the server side. The new owner ID will be sent to all clients so that they are aware of who is the new owner. The owner of the object is the one allowed to Trigger SNetworkActions to be executed remotely. Use only on objects that are already spawned. Otherwise you can set the owner when spawning with Spawn(client_id);
Last updated