docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Use a script to access the active multiplayer role

    You can access the multiplayer role that the Unity Editor or the current build uses in Play mode in a script. This allows you to write custom logic that runs only on the server or only on the client.

    The example below demonstrates custom logic that exists when a process runs on the server or as a client.

    using UnityEngine;
    using UnityEngine.SceneManagement;
    using Unity.Multiplayer;
    
    public class SceneBootstrap : MonoBehaviour
    {
        void Start()
        {
            var role = MultiplayerRolesManager.ActiveMultiplayerRoleMask;
    
            if (role == MultiplayerRoleFlags.Server)
            {
                Debug.Log("Running SceneBootstrap in the server");
                SceneManager.LoadScene("ServerScene");
            }
            else if (role == MultiplayerRoleFlags.Client)
            {
                Debug.Log("Running SceneBootstrap in a client");
                SceneManager.LoadScene("LobbyScene");
            }
        }
    }
    
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)