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

Prefab not destroyed when reconnecting

Discussion in 'Multiplayer' started by FuzzyShan, Jun 7, 2017.

  1. FuzzyShan

    FuzzyShan

    Joined:
    Jul 30, 2012
    Posts:
    182
    say I have disconnected my client, but server still keeps track of the old spawn player object and connection. When I reconnect using a new network client, is there a way to make the old spawn object network identity to sync with my client object.

    1. If I don't generate an new spawn object network identity through server, I won't able to receive all the old objects on server. Therefore I had to spawn a new object.

    2 is there a way to make the old object to sync with a new object i generated in client using some llapi or hapi stuff.

    3. If I don't do step 2, both old object and new object now shows up on both client and server after someboreconnect, how would I go removing the new object and use the old object for syncing? Or should I just make the new object where the old object is and destroy the old object after?
     
  2. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    In your NetworkManager, override OnServerDisconnect and don't call the base implelmentation. That will leave your old player object there.

    To reconnect to it you have to do a bit more work. First, you need to keep track of the identity of the player object in some way, such as netId, or a persistent unique id that you assign. You also need to track the identity of the player perhaps by user name, an account or by IP. Associate the two values together and then when your player reconnects, you can call ReplacePlayerForConnection and give it the old player. I do this, but I let the NetworkManager spawn a player object that just contains all my commands and things so there's no old player object to destroy. If there was no old player object, then the server spawns a new one.
     
  3. FuzzyShan

    FuzzyShan

    Joined:
    Jul 30, 2012
    Posts:
    182
    Much Thanks, That's where I am getting close to, I always thought ReplacePlayerForConnection would be replacing a new spawned object to an old connection but not other way around.
    I am confused by the last sentence you said. basically I use network.Transport to send rather than high level commands for most of my connections, but when 3D world objects communicate with 3D world Objects, I use HLAPI. Currently if I do have an old object with network Identity, when I connect with a new client, how would I able to make sure the new client able to communicate with old object?

    Are you saying:
    1. replace the new connection's gameobject by pass in old player object.
    2. then do Network spawn again, since we already have the old player object all other class wouldn't spawn anything, only the new client will receive everything along with the old object?
     
  4. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    First things first. You say you're using the LLAPI for most stuff but the HLAPI for 3D objects, so you do at least have an HLAPI server started? All this applies to that scenario...

    ReplacePlayerForConnection doesn't really replace the old one, although you're free to destroy it after it's been replaced. It is still there, still has authority, even still claims to be the local player. But so will the one you "Replace" it with. The original player you spawn doesn't even have to have anything but a GameObject and NetworkIdentity, so if you always spawn a dummy player that doesn't do anything but handle HLAPI messages, it's completely throw-away.

    1. Yes
    2. You don't spawn it again. It's already spawned. You're just taking control of it by making it a player object.
     
  5. FuzzyShan

    FuzzyShan

    Joined:
    Jul 30, 2012
    Posts:
    182
    Regarding to statement 2, If I don't spawn it, how am I able to pick up all the spawned objects within the new client?
     
  6. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    When the new client joins the scene, the objects that are spawned on server are spawned for you by the HLAPI.
     
  7. FuzzyShan

    FuzzyShan

    Joined:
    Jul 30, 2012
    Posts:
    182
    That's interesting, I don't spawn anything, until I actually have one network identity object spawned, might have to do with how you connect to server. I pretty much just took the code and use high level API to connect to server using new client(), client.connect(). I got the connection is connected, but I override most of their internal functions, which result that I don't see any objects.
     
  8. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    Only objects with a NetworkIdendity can be spawned on the network. You create the object on the server side and pass it to NetworkServer.Spawn. Then it will be spawned for you on the clients.