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

OnServerAddPlayer not called on client joining?

Discussion in 'Multiplayer' started by ArkVoid, Nov 8, 2015.

  1. ArkVoid

    ArkVoid

    Joined:
    Mar 4, 2014
    Posts:
    3
    I'm trying to set a name for a player and I do the logic of assigning the name (Not the syncing which happens in other code) in the OnServerAddPlayer method which takes the name from a text input. It all works when I write a name and host a server, the problem happens when a client joins the hosted server, it manages to get the host player name but it doesn't set his own name - meaning the OnServerAddPlayer didn't executed when the new client is joining - I ran a debug and found out that It really doesn't call it.

    Any suggestions where should I put the logic of getting the name out the input field and setting it on the player prefab instead on OnServerAddPlayer?
     
  2. Skerbey

    Skerbey

    Joined:
    Oct 15, 2015
    Posts:
    11
    OnServerAddPlayer is called on the server every time an AddPlayer message is sent to the server. The clients will not run it. I'm assuming you are allowing the clients to set their own names before they join a game in which case you could have the NetworkManager store the players name when StartHost() and StartClient() are called and attach a NetworkBehavior script to the player that grabs this value for the local player and syncs it.

    Is that what you intended to do?

    Edit: Use this function to set it on the local player:
    Code (CSharp):
    1. public override void OnStartLocalPlayer()
     
    Last edited: Nov 9, 2015
    CrandellWS and ArkVoid like this.
  3. ArkVoid

    ArkVoid

    Joined:
    Mar 4, 2014
    Posts:
    3
    Thanks! overriding OnStartLocalPlayer in my player setup script did the trick :)
     
    CrandellWS likes this.