Skip to main content

Configure a Player to run as a server, client, or host example

Without configuration, tags don't do anything. You must target tags in your game scripts with CurrentPlayer.Tag. You can place these scripts where you want, but you must attach the scripts to a NetworkObject (such as the Player).

info

The following example uses the Contains method, which is case-sensitive by default. You can make it case-insensitive by passing the System.StringComparison.CurrentCultureIgnoreCase method.

Run as a server, client, or host

The following script uses the Netcode for GameObjects (NGO) NetworkManager to automatically connect the Virtual Player as a server, client, or host based on their tag. A Player with the Server tag automatically runs as a server, and a Player with the Client tag automatically runs as a client.

note
using Unity.Netcode;
using UnityEngine;
using Unity.Multiplayer.Playmode;

/// A MonoBehaviour to automatically start Netcode for GameObjects
/// clients, hosts, and servers
public class MppmConnect : MonoBehaviour
{
void Start()
{
var mppmTag = CurrentPlayer.Tag;
var networkManager = NetworkManager.Singleton;
if (mppmTag.Contains("Server"))
{
networkManager.StartServer();
}
else if (mppmTag.Contains("Host"))
{
networkManager.StartHost();
}
else if (mppmTag.Contains("Client"))
{
networkManager.StartClient();
}
}
}