Reference: https://mirror-networking.gitbook.io/docs/
Reference: https://mirror-networking.gitbook.io/docs/guides/networkbehaviour
Using ...;
Using ...;
Using Mirror;
// public class Example : MonoBehaviour
public class Example : NetworkBehaviour
- When a new client is connected to the network, a new player instance is spawned on all connected clients.
- There will be many player game objects in the scene.
- In
player.cs
public class player : NetworkBehaviour { void Example() { // Code applied to the local player and all other players if (isLocalPlayer) { // Code applied to the local player only } } }
- Since the player is now spawned dynamically, any game object reference (such as ScoreDisplay) should be assigned with code (not dragged from the scene)
Example: SpawnFood() in GameManager.cs
/* When we want to spawn a game object */
// Spawn only at the server
if (!isServer) return;
var newObject = Instantiate(prefab, transform.position, transform.rotation);
// Tell other clients to spawn the game object
NetworkServer.Spawn(newObject);
- Game object to be synced between all clients must be spawned only at the server and then synced to the clients
- Game object should spawned from prefab, not GameObject instance from the scene
- There should not be any game object instance in the scene because the instance will be independent in each client (not synced)
- Never spawn game object as child to other game object, because
NetworkServer.Spawn()
takes its local transform as global transform.- If there is a need, we will have to write a custom spawn function
- Add
Volume.cs
toGameManager
or any other game object - Access the volume:
// In GameManager (or any game object that Volume.cs is attached to) Volume vol = gameObject.GetComponent<Volume>(); // In other game object Volume vol = FindObjectOfType<Volume>(); // Example Usage Debug.log(vol.volume); // vol.volume is a float