Search Unity

isServer is true for the host and the other client (running on a different device)

Discussion in 'Multiplayer' started by shivansh11, Mar 15, 2018.

  1. shivansh11

    shivansh11

    Joined:
    Jul 28, 2014
    Posts:
    5
    Hey guys,

    I'm using UNET's MatchMaking service. First, I create a room in the Unity Editor itself and then I check the Network Information of my primary PlayerPrefab...it's TRUE (or YES) for isClient and isServer both. So far so good.

    Then I join the same room from a different device (Android phone). The Unity Editor now shows two PlayerPrefabs (one for the editor and one for the Android phone), both having isClient and isServer TRUE in their Network Information section.

    From what I know and have gleaned so far, isServer should be TRUE only for the Unity Editor's PlayerPrefab (assuming it created the room and became the host).

    My code depends a lot on isServer, all the commands intended for only Server get's executed by clients too and that's not what I want.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    isServer should be "true" on all NetworkBehaviour scripts run on the server. That includes player objects representing non-server clients. You use isServer to differentiate what code in a script is run on the server or not, not to determine if a player object represents a server player or client player.
     
    shivansh11 likes this.
  3. shivansh11

    shivansh11

    Joined:
    Jul 28, 2014
    Posts:
    5
    Then what should I use to differentiate a server (or a host) from a normal client?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You use isServer to differentiate a server from a client. What you really want though is an entirely different question. "How do you differentiate a Player object that represents the host computer from the other player objects on the clients?"

    Easy way is to just set a SyncVar on the server that you check on the clients

    Code (csharp):
    1.  
    2. [SyncVar]
    3. public bool ThisIsTheServerPlayer = false;
    4.  
    5. void Start()
    6. {
    7.     if (isLocalPlayer && isServer)
    8.     {
    9.         ThisIsTheServerPlayer = true;
    10.     }
    11. }
     
  5. shivansh11

    shivansh11

    Joined:
    Jul 28, 2014
    Posts:
    5
    Thank you :)
     
  6. Tiberius1701

    Tiberius1701

    Joined:
    Sep 16, 2014
    Posts:
    71
    Thank you! This helped so much!