Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Unity Multiplayer Samples Utilities Package Error

Discussion in 'General Discussion' started by Deleted User, May 20, 2023.

  1. Deleted User

    Deleted User

    Guest

    Hello !!

    So, I started messing around with the Multiplayer Aspect of Unity, I was following Code Monkey's Video on Multiplayer, I got away with most of them things but, when it was time for the client to move around he had installed a whole new Package from the git link from the "Unity Network Transform", he said the version of Unity must be above 2021.3.10, I am working on the version 2021.3.21f1f, but when I installed the package, I got 6 errors and all of them had to do with OnClientStarted, OnClientStopped and OnServerStopped.

    Code (CSharp):
    1. using System;
    2. using Unity.Netcode;
    3. using UnityEngine.Networking;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. namespace Unity.Multiplayer.Samples.Utilities
    8. {
    9.     public class SceneLoaderWrapper : NetworkBehaviour
    10.     {
    11.         /// <summary>
    12.         /// Manages a loading screen by wrapping around scene management APIs. It loads scene using the SceneManager,
    13.         /// or, on listening servers for which scene management is enabled, using the NetworkSceneManager and handles
    14.         /// the starting and stopping of the loading screen.
    15.         /// </summary>
    16.  
    17.         [SerializeField]
    18.         ClientLoadingScreen m_ClientLoadingScreen;
    19.  
    20.         [SerializeField]
    21.         LoadingProgressManager m_LoadingProgressManager;
    22.  
    23.         bool IsNetworkSceneManagementEnabled => NetworkManager != null && NetworkManager.SceneManager != null && NetworkManager.NetworkConfig.EnableSceneManagement;
    24.  
    25.         bool m_IsInitialized;
    26.  
    27.         public static SceneLoaderWrapper Instance { get; protected set; }
    28.  
    29.         public virtual void Awake()
    30.         {
    31.             if (Instance != null && Instance != this)
    32.             {
    33.                 Destroy(gameObject);
    34.             }
    35.             else
    36.             {
    37.                 Instance = this;
    38.             }
    39.             DontDestroyOnLoad(this);
    40.         }
    41.  
    42.         public virtual void Start()
    43.         {
    44.             SceneManager.sceneLoaded += OnSceneLoaded;
    45.             NetworkManager.OnServerStarted += OnNetworkingSessionStarted;
    46.             NetworkManager.OnClientStarted += OnNetworkingSessionStarted;
    47.             NetworkManager.OnServerStopped += OnNetworkingSessionEnded;
    48.             NetworkManager.OnClientStopped += OnNetworkingSessionEnded;
    49.         }
    50.  
    51.         void OnNetworkingSessionStarted()
    52.         {
    53.             // This prevents this to be called twice on a host, which receives both OnServerStarted and OnClientStarted callbacks
    54.             if (!m_IsInitialized)
    55.             {
    56.                 if (IsNetworkSceneManagementEnabled)
    57.                 {
    58.                     NetworkManager.SceneManager.OnSceneEvent += OnSceneEvent;
    59.                 }
    60.  
    61.                 m_IsInitialized = true;
    62.             }
    63.         }
    64.  
    65.         void OnNetworkingSessionEnded(bool unused)
    66.         {
    67.             if (m_IsInitialized)
    68.             {
    69.                 if (IsNetworkSceneManagementEnabled)
    70.                 {
    71.                     NetworkManager.SceneManager.OnSceneEvent -= OnSceneEvent;
    72.                 }
    73.  
    74.                 m_IsInitialized = false;
    75.             }
    76.         }
    77.  
    78.         public override void OnDestroy()
    79.         {
    80.             SceneManager.sceneLoaded -= OnSceneLoaded;
    81.             if (NetworkManager != null)
    82.             {
    83.                 NetworkManager.OnServerStarted -= OnNetworkingSessionStarted;
    84.                 NetworkManager.OnClientStarted -= OnNetworkingSessionStarted;
    85.                 NetworkManager.OnServerStopped -= OnNetworkingSessionEnded;
    86.                 NetworkManager.OnClientStopped -= OnNetworkingSessionEnded;
    87.             }
    88.             base.OnDestroy();
    89.         }
    90.  
    91.         /// <summary>
    92.         /// Loads a scene asynchronously using the specified loadSceneMode, with NetworkSceneManager if on a listening
    93.         /// server with SceneManagement enabled, or SceneManager otherwise. If a scene is loaded via SceneManager, this
    94.         /// method also triggers the start of the loading screen.
    95.         /// </summary>
    96.         /// <param name="sceneName">Name or path of the Scene to load.</param>
    97.         /// <param name="useNetworkSceneManager">If true, uses NetworkSceneManager, else uses SceneManager</param>
    98.         /// <param name="loadSceneMode">If LoadSceneMode.Single then all current Scenes will be unloaded before loading.</param>
    99.         public virtual void LoadScene(string sceneName, bool useNetworkSceneManager, LoadSceneMode loadSceneMode = LoadSceneMode.Single)
    100.         {
    101.             if (useNetworkSceneManager)
    102.             {
    103.                 if (IsSpawned && IsNetworkSceneManagementEnabled && !NetworkManager.ShutdownInProgress)
    104.                 {
    105.                     if (NetworkManager.IsServer)
    106.                     {
    107.                         // If is active server and NetworkManager uses scene management, load scene using NetworkManager's SceneManager
    108.                         NetworkManager.SceneManager.LoadScene(sceneName, loadSceneMode);
    109.                     }
    110.                 }
    111.             }
    112.             else
    113.             {
    114.                 // Load using SceneManager
    115.                 var loadOperation = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
    116.                 if (loadSceneMode == LoadSceneMode.Single)
    117.                 {
    118.                     m_ClientLoadingScreen.StartLoadingScreen(sceneName);
    119.                     m_LoadingProgressManager.LocalLoadOperation = loadOperation;
    120.                 }
    121.             }
    122.         }
    123.  
    124.         void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
    125.         {
    126.             if (!IsSpawned || NetworkManager.ShutdownInProgress)
    127.             {
    128.                 m_ClientLoadingScreen.StopLoadingScreen();
    129.             }
    130.         }
    131.  
    132.         void OnSceneEvent(SceneEvent sceneEvent)
    133.         {
    134.             switch (sceneEvent.SceneEventType)
    135.             {
    136.                 case SceneEventType.Load: // Server told client to load a scene
    137.                     // Only executes on client
    138.                     if (NetworkManager.IsClient)
    139.                     {
    140.                         // Only start a new loading screen if scene loaded in Single mode, else simply update
    141.                         if (sceneEvent.LoadSceneMode == LoadSceneMode.Single)
    142.                         {
    143.                             m_ClientLoadingScreen.StartLoadingScreen(sceneEvent.SceneName);
    144.                             m_LoadingProgressManager.LocalLoadOperation = sceneEvent.AsyncOperation;
    145.                         }
    146.                         else
    147.                         {
    148.                             m_ClientLoadingScreen.UpdateLoadingScreen(sceneEvent.SceneName);
    149.                             m_LoadingProgressManager.LocalLoadOperation = sceneEvent.AsyncOperation;
    150.                         }
    151.                     }
    152.                     break;
    153.                 case SceneEventType.LoadEventCompleted: // Server told client that all clients finished loading a scene
    154.                     // Only executes on client
    155.                     if (NetworkManager.IsClient)
    156.                     {
    157.                         m_ClientLoadingScreen.StopLoadingScreen();
    158.                         m_LoadingProgressManager.ResetLocalProgress();
    159.                     }
    160.                     break;
    161.                 case SceneEventType.Synchronize: // Server told client to start synchronizing scenes
    162.                 {
    163.                     // todo: this is a workaround that could be removed once MTT-3363 is done
    164.                     // Only executes on client that is not the host
    165.                     if (NetworkManager.IsClient && !NetworkManager.IsHost)
    166.                     {
    167.                         // unload all currently loaded additive scenes so that if we connect to a server with the same
    168.                         // main scene we properly load and synchronize all appropriate scenes without loading a scene
    169.                         // that is already loaded.
    170.                         UnloadAdditiveScenes();
    171.                     }
    172.                     break;
    173.                 }
    174.                 case SceneEventType.SynchronizeComplete: // Client told server that they finished synchronizing
    175.                     // Only executes on server
    176.                     if (NetworkManager.IsServer)
    177.                     {
    178.                         // Send client RPC to make sure the client stops the loading screen after the server handles what it needs to after the client finished synchronizing, for example character spawning done server side should still be hidden by loading screen.
    179.                         StopLoadingScreenClientRpc(new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIds = new[] { sceneEvent.ClientId } } });
    180.                     }
    181.                     break;
    182.             }
    183.         }
    184.  
    185.         void UnloadAdditiveScenes()
    186.         {
    187.             var activeScene = SceneManager.GetActiveScene();
    188.             for (var i = 0; i < SceneManager.sceneCount; i++)
    189.             {
    190.                 var scene = SceneManager.GetSceneAt(i);
    191.                 if (scene.isLoaded && scene != activeScene)
    192.                 {
    193.                     SceneManager.UnloadSceneAsync(scene);
    194.                 }
    195.             }
    196.         }
    197.  
    198.         [ClientRpc]
    199.         void StopLoadingScreenClientRpc(ClientRpcParams clientRpcParams = default)
    200.         {
    201.             m_ClientLoadingScreen.StopLoadingScreen();
    202.         }
    203.     }
    204. }
    205.  
    The error is in lines 46, 47, 48 and 84, 85, 86

     
  2. Deleted User

    Deleted User

    Guest

    Console:
     

    Attached Files:

  3. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    656
    It sounds like you need to update your Netcode for Game Objects package to 1.4.0. Those callbacks were added in that version and it looks like Samples was updated to take advantage of them.
     
    Beatdones likes this.