Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Spawn Network Object on selected clients only...

Discussion in 'Netcode for GameObjects' started by Altalus06, Jun 4, 2021.

  1. Altalus06

    Altalus06

    Joined:
    Sep 3, 2017
    Posts:
    10
    Hello everyone,

    I am facing a problem concerning the spawn of network objects on new clients when the object is already spawned.

    To explain the problem:
    - A client #1 connects to the server, which then instantiates a new NetworkObject from a prefab (NetworkManager is in PrefabSync mode), and then call to the new "NetworkObject.Spawn" method.
    - The object is replicated to client #1
    - A client #2 then connects. I need him to have this object replicated too

    but if I'm right, the only way to do that would be to call a "Despawn" and then a "Spawn" again...pretty inefficient :(

    Isn't there a way to trigger the spawn of this object only on this new client #2, without having to despawn it from the client #1 who already have it?

    Thanks in advance for your help
     
  2. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    182
    A newly connected client should get all existing objects spawned automatically. There should be no need to de-spawn and respawn again.
     
  3. Altalus06

    Altalus06

    Joined:
    Sep 3, 2017
    Posts:
    10
    Even in prefabsync mode and with scene management disabled ?

    Currently, the object is spawned on client #1 (I need to call the networkobject' Spawn method server side to do this).
    But when client#2 connects to the server, the object is never automatically replicated on this newly client (unless I call despawn and spawn again)
     
    Last edited: Jun 4, 2021
  4. Altalus06

    Altalus06

    Joined:
    Sep 3, 2017
    Posts:
    10
    OK, finally, problem solved.

    The problem came from the fact that client side, I was changing current Scene just after player has connected to server.

    And I was calling "DontDestroyOnLoad" at Awake of the networkObject, to avoid having it destructed due to scene change. Testing first if NetworkObject was Client or server side.

    Except that the first Network Object was replicated before "NetworkManager.Singleton.IsConnectedClient" returns true.
    So, this object was destroyed immediatly (that's why I was thinking the object was not replicated)

    Now everything is working fine now :)
     
unityunity