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.
  2. Dismiss Notice

Question Transfert ownership on owner disconnect

Discussion in 'Netcode for GameObjects' started by Pitou22, Nov 15, 2021.

  1. Pitou22

    Pitou22

    Joined:
    Sep 23, 2015
    Posts:
    26
    Hello,
    I'm building a coop game with MLAPI 0.1.0, unity 2020.3.22f1. Each player owns some units, and only him has the control on them, so I gave him the ownership on the NetworkObject component.

    My issue is that if a player disconnect (for any reason), I don't want to destroy its units (actual behaviour), but I want to transfert the ownership to another player (by default to the host).
    What is the proper way to obtain this behaviour? I know how to change the ownership, but I need to do this before MLAPI has already destroyed the object. I thought of a workaround: that will be to have all units owned by the server and building my own "sub-ownership" system to allow actions only for the player that has my "sub-ownership".
     
  2. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    `NetworkObject` has a `DontDestroyWithOwner` bool, set that to true on the server and the object will not be destroyed when the owner disconnects.
     
    Pitou22 likes this.
  3. raggnic

    raggnic

    Joined:
    Sep 27, 2017
    Posts:
    13
    I'm using this property for spawned objects.
    At first they are owned by the server, and a client can ask for ownership.
    If he gets the ownership he can move the objet.

    If the user leaves the session with DontDestroyWithOwner the networkObject remains with the former owner id.
    How can i automatically remove the ownership and give it back to server?