Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Issue with Lobby - Unable to Create New Lobby After Playing a Match

Discussion in 'Netcode for GameObjects' started by WesleydG, May 31, 2023.

  1. WesleydG


    May 13, 2020

    I'm encountering a bug in my game where I'm unable to create a new lobby after playing a game. The issue seems to occur only when I've already played a match, as it works fine when I haven't played any matches yet.

    The error message I received is as follows:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Unity.Netcode.NetworkBehaviour.__endSendClientRpc (Unity.Netcode.FastBufferWriter& bufferWriter, System.UInt32 rpcMethodId, Unity.Netcode.ClientRpcParams clientRpcParams, Unity.Netcode.RpcDelivery rpcDelivery) (at Library/PackageCache/com.unity.netcode.gameobjects@1.4.0/Runtime/Core/NetworkBehaviour.cs:202)
    3. LobbyManager.UpdatePlayerClientRpc (System.UInt64 clientId) (at Assets/Scripts/MainMenu/Network/Lobby/LobbyManager.cs:114)
    4. LobbyManager.PropagateToClients () (at Assets/Scripts/MainMenu/Network/Lobby/LobbyManager.cs:107)
    5. LobbyManager.OnClientConnectedCallback (System.UInt64 playerId) (at Assets/Scripts/MainMenu/Network/Lobby/LobbyManager.cs:96)
    6. Unity.Netcode.NetworkManager.InvokeOnClientConnectedCallback (System.UInt64 clientId) (at Library/PackageCache/com.unity.netcode.gameobjects@1.4.0/Runtime/Core/NetworkManager.cs:446)
    7. Unity.Netcode.NetworkManager.StartHost () (at Library/PackageCache/com.unity.netcode.gameobjects@1.4.0/Runtime/Core/NetworkManager.cs:1021)
    8. LobbyManager.CreateLobby (LobbyData data) (at Assets/Scripts/MainMenu/Network/Lobby/LobbyManager.cs:58)
    9. System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <3dd5df5ef4974f29afeb2d3ba227c5da>:0)
    10. UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <790a43858b4b404fbe02a2b7b4a0d9d6>:0)
    11. UnityEngine.UnitySynchronizationContext.Exec () (at <790a43858b4b404fbe02a2b7b4a0d9d6>:0)
    12. UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <790a43858b4b404fbe02a2b7b4a0d9d6>:0)
    Here is the code that triggers leaving the match:
    Code (CSharp):
    1. public async void QuitToMainMenu()
    2. {
    3.     await MatchmakingService.LeaveLobby();
    4.     NetworkManager.Singleton.Shutdown();
    5.     SceneManager.LoadScene((int)SceneIndexes.TITLE_SCREEN);
    6. }
    I already tried a lot of things, but I'm lost.

    I would appreciate any insights or suggestions on how to resolve this issue. If you need any additional information or code snippets, please let me know, and I'll be happy to provide them.

    Thank you in advance for your assistance.

    - Wesley

    Unity version: 2022.1.18f1
    Netcode for Gameobjects version: 1.4.0
    Lobby: 1.0.3
    Relay: 1.0.5
  2. RikuTheFuffs-U


    Unity Technologies

    Feb 20, 2020
    Hi @WesleydeGraaf , are you sure you subscribe/unsubscribe events properly (evenly) from LobbyManager.OnClientConnectedCallback between sessions? It seems like you're calling a method on a destroyed object.
  3. WesleydG


    May 13, 2020
    I fixed the issue. I subscribed to the OnClientConnectedCallback, but when the object got destroyed (Scene switch). I didn't unsubscribe from the event.

    Thanks for helping!

    (I don't know why I didn't see that. I tried a lot of things :confused:)
    RikuTheFuffs likes this.