Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unity Multiplayer [Mirror] Objects persisting on clients after NetworkServer.Destroy() on server

Discussion in 'Multiplayer' started by gtramnull, Jun 10, 2020.

  1. gtramnull

    gtramnull

    Joined:
    Apr 23, 2020
    Posts:
    5
    Hi there,

    So first of all, my project is heavily based on Mirror's sample Room. Now, I'm running into an issue where I'm destroying all room objects using NetworkServer.Destroy(roomPlayer) after being replaced with GamePlayer (copying over some data such as name etc). Once all players get in GameScene from LobbyScene the server-client player looks good, however, the local client players seem to be destroyed properly but every other LobbyPlayer sticks around causing this huge UI overlap issue I'm having.

    Here's an example(images attached too):

    Lobby Scene:
    • lobbyplayer1(client-server)
    • lobbyplayer2
    • lobbyplayer3
    Game Scene:
    • gameplayer1(client-server)
    • gameplayer2, lobbyplayer1
    • gameplayer3, lobbyplayer1, lobbyplayer2
    Code (CSharp):
    1.  
    2. public override GameObject OnRoomServerCreateGamePlayer(NetworkConnection conn, GameObject roomPlayer) {
    3.         var gamePlayerInstance = Instantiate(playerPrefab);
    4.         gamePlayerInstance.GetComponent<NetworkGamePlayerExt>().SetDisplayName(roomPlayer.GetComponent<NetworkRoomPlayer>().displayName);
    5.  
    6.         NetworkServer.Destroy(roomPlayer); //this is only destroying roomPlayers on client-server and local clients on clients
    7.  
    8.         return gamePlayerInstance.gameObject;
    9. }

    My goal is to simply get rid of roomplayers once they get swapped with gameplayers. Sounds simple but I feel like there's something behind the scenes in the whole NetworkRoomManager/NetworkRoomPlayer I'm not aware of and that's messing it all up.


    Any help is appreciated.
    Thanks!
     

    Attached Files:

  2. erenbaltali16

    erenbaltali16

    Joined:
    Apr 13, 2020
    Posts:
    2
    Did you managed to solve it?
     
    reachout likes this.
  3. DerperDoing

    DerperDoing

    Joined:
    Mar 19, 2018
    Posts:
    2
    Was any of you able to solve this? I'm facing this issue as well.
     
  4. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    4,283
  5. SandaruwanFdo

    SandaruwanFdo

    Joined:
    Jan 11, 2021
    Posts:
    1
    Any solution? same issue
     
  6. JikaiNoTsubasa

    JikaiNoTsubasa

    Joined:
    Apr 28, 2016
    Posts:
    2
    Same issue here, can't get rid of the other clients room objects even after destroy them in the createplayer function..

    In the "Client-Host" instance, all room players have been deleted. It Seems it works on the host but is not propagated to other clients

    EDIT:
    Here a solution, in the NetworkRoomManager:
    Code (CSharp):
    1. public override bool OnRoomServerSceneLoadedForPlayer(NetworkConnection conn, GameObject roomPlayer, GameObject gamePlayer)
    2.         {
    3.             Debug.LogWarning("Destroying RoomPlayer " + roomPlayer.name);
    4.             Destroy(roomPlayer, 1); // Mandatory
    5.             return true;
    6.         }
    The room player is kept as don't destroy on load because if you want to go back to the room and keep the data. You can delete it ones the scene is fully loaded for the player object, which is not totally finished after OnRoomServerSceneLoadedForPlayer. That's why we run Destroy(roomPlayer,1)
     
    Last edited: Jan 10, 2022