Search Unity

Variable or Function for Who is Host

Discussion in 'Multiplayer' started by dalblas, May 24, 2017.

  1. dalblas

    dalblas

    Joined:
    Apr 9, 2014
    Posts:
    10
    Hi there!

    Is there a variable that gets set or a function that I can call to tell if my current player is the host or not. I have tried using isServer, but isServer is true also on my client.

    Thanks,

    Dan
     
  2. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    If you are using the peer-hosted pattern (where one of your clients acts as both the server AND the client) which you will be by default, then one of your clients will have isServer set to true, while all other clients will have isServer set to false.

    Try it. Try having two or more clients connect to the same network. The first client will have isServer set to true, while the others will have isServer set to false.
     
  3. dalblas

    dalblas

    Joined:
    Apr 9, 2014
    Posts:
    10
    isServer.jpg I have tried that, but each of my clients that connect to the host has isServer = true as well. I have attached a screenshot to show that.
     
    Noxury likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    isServer checks if this unity instance is the server. You should notice that when you have the editor acting as the server, all isServer calls on all gameobjects are True. On all your clients which are not hosting the game, all isServer calls are False.

    So if you want all clients to know which player is the player on the host computer, here's what I'd do. In the player prefab, maybe in some script in start, I'd check if both isLocalPlayer and isServer are both True. If both are True that means this is the player on the host. Then I'd set a syncvar bool, so all the clients will also know this is the server player. There may be an easier way to do this, but I haven't had to differentiate the host player from the other clients before.
     
    brainwipe likes this.
  5. dalblas

    dalblas

    Joined:
    Apr 9, 2014
    Posts:
    10
    Thanks for the response! I finally understand the meaning of isServer now. It is quite the thing to wrap your head around, but I finally understand now and have working code.