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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UNET: NetworkServer.Spawn

Discussion in 'UNet' started by RicheyMB, Aug 31, 2016.

  1. RicheyMB

    RicheyMB

    Joined:
    Jul 20, 2009
    Posts:
    111
    I'm having a problem with my game and need to understand the Spawn method a little better. Hoping someone can help me achieve this.

    <TLDR> Why does NetworkServer.Spawn not create a duplicate on the server. </TLDR>

    From what I see most examples do the following

    Code (CSharp):
    1. [Command]
    2. public void CmdSpawnSomething()
    3. {
    4.    var objectToSpawn = Instantiate(prefab);
    5.  
    6.    //set up objectToSpawn
    7.  
    8.    NetworkServer.Spawn(objectToSpawn);
    9. }
    from what I understand is this will run on the server (command) and create an instance on all clients.

    My question is - why does this not create a duplicate 'objectToSpawn' on the server?
     
  2. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    Why should it? I don't think such functionality would make too much sense. Object instantiation and spawning over network seems to be uncoupled by design for good reason.

    Note that you can have a look at the NetworkServer source if you want to retrace exactly how it works.
     
    Last edited: Aug 31, 2016
  3. RicheyMB

    RicheyMB

    Joined:
    Jul 20, 2009
    Posts:
    111
    Thanks for the clarification.

    The problem I'm having is that a duplicate is being created. It doesn't happen with any other objects where I'm using the same pattern but I've always had a nagging doubt because this code reads like it will create a duplicate (even though that wouldn't make sense)

    Thanks for the reminder that the source is on bitbucket. Very useful.
     
  4. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    Okay, now you got me confused. o_O
    What is the actual problem?
     
  5. RicheyMB

    RicheyMB

    Joined:
    Jul 20, 2009
    Posts:
    111
    So sorry - missed the notification on this post and feel very rude.

    My issue was nothing to do with NetworkServer.Spawn. I was actually instantiating the object again on the client in rare circumstances that was hard to debug. Found myself drawn into the red herring that NeworkServer.Spawn might create another instance.
     
  6. U-GeeN

    U-GeeN

    Joined:
    Mar 22, 2015
    Posts:
    95
    Since we are on the NetworkServer.Spawn topic...
    Say I unspawn a player on defeat via NetworkServer.Unspawn and disable colliders, renderers and stuff on the server.
    For the next round I want to respawn all defeated players via NetworkServer.Spawn (after enabling disabled stuff).
    The problem is that no client has control over his player object.
    Does anybody know how to make sure that every player-client gets his player object spawned that he can control?
    Oh, and I have nothing checked on the Network Identity, if that matters...
     
  7. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Wouldn't you use NetworkServer.SpawnWithClientAuthority ?
     
  8. U-GeeN

    U-GeeN

    Joined:
    Mar 22, 2015
    Posts:
    95
    Yea that might work...
    But silly question: how do I get the right player or connection for that fnction?