Search Unity

Spawning Correct player prefab across clients

Discussion in 'Multiplayer' started by gridGuy, Sep 2, 2017.

  1. gridGuy

    gridGuy

    Joined:
    Sep 2, 2017
    Posts:
    4
    Hello all, I've managed to throw together a very robust character selection screen in my game that works offline, and then, after selecting a character, the player is brought to the next scene which has a network manager, I have a simple script called "character transport" that grabs the Network Manager from my Network Manager object, and then tells it to change the player prefab to the selected character, this works to an extent, but then once any other player joins the server during run time, all other players are given the same player prefab. Wondering if there is a simple remedy for this or a much deeper understanding of how registering and controlling networks is required before moving forward?

    public void Start(){

    NetworkManager networkManager = GameObject.Find ("Network Manager").GetComponent<NetworkManager> ();
    networkManager.playerPrefab = GameObject.Find ("mechManager").GetComponent<MechManager> ().mech;

    }


    Above is the very simple code that does infact change the player prefab, it just doesn't work once trying to spawn it on the network,
    (that is, it spawns whatever the host chose before the client, rather than the client's selection being spawned.)
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You need to ask for a server to instantiate a specific player prefab via AddPlayerInternal method. Otherwise it will be the same player prefab all the time.

    Override OnServerAddPlayer and/or OnServerAddPlayerInternal to change how player is instantiated.
    Here's link to the NetworkManager source, so you could understand what's going on:
    https://bitbucket.org/Unity-Technol...ger.cs?at=2017.1&fileviewer=file-view-default
     
    TwoTen likes this.
  3. gridGuy

    gridGuy

    Joined:
    Sep 2, 2017
    Posts:
    4
    Thank you I got it to work!
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just wanted to mention an alternative strategy I'm using in my game. All player prefabs are the same for all players, but the player prefab doesn't actually represent the player in the game. I spawn a separate prefab of whatever I want that I reference. When the player does keypresses and other input I handle them on the player prefab, but I manipulate the other gameobject I spawned.

    I did it this way for 2 reasons. The first is the question in this thread, and I hadn't seen an answer at the time I implemented this. The second though is how to handle disconnects. I know you can change what happens to the player object on disconnect, but it is really straight forward to just go with the default behavior of the player object automatically destroys on disconnect and I can just leave this other object that appears to be the player in the game for as long as I want. On reconnect I just hook the reference back up to the existing object in the scene and the player just picks up where they left off (MMO game where I didn't want players to disconnect as a way of running from other players, so I keep them in the game for a couple minutes when they disconnect).