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 - Trying to understand this behaviour

Discussion in 'UNet' started by IvanHTN, Sep 30, 2016.

  1. IvanHTN

    IvanHTN

    Joined:
    Feb 12, 2016
    Posts:
    37
    Hi everyone, i need help understanding this behaviour:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class Character : NetworkBehaviour {
    5.  
    6.      [SyncVar (hook = "refreshHP")] public float currHP;
    7.  
    8.      public void Start () {
    9.           currHP = 100f;
    10.      }
    11.  
    12.      void refreshHP (float current) {
    13.           if (isLocalPlayer) {
    14.                 Debug.log ("Local");
    15.           } else {
    16.                 Debug.log ("Other");
    17.           }
    18.       }
    19.  
    20. }
    Now if i'm the host in the unity inspector i get 1 "local" debug. But if i enter with a client from the build version i get 1 "other" debug.

    Can someone explain why this is happening? I was expecting the client to also reach the isLocalPlayer condition. Should i assume client is never reaching isLocalPlayer?
     
    Last edited: Sep 30, 2016
  2. Crizz92

    Crizz92

    Joined:
    Sep 17, 2015
    Posts:
    44
    I'm not sure to understand what u mean and what u want.

    But isLocalPlayer just give you the information if the current object is the one on your local machine. So if you check when you connect 2 players on the server, you will see in the Unity scene 2 instance of the player. One is your, the other one is the one of the other player.
     
  3. IvanHTN

    IvanHTN

    Joined:
    Feb 12, 2016
    Posts:
    37
    Yea i understand i have two copies of the same script running in the scene.

    My wonders are why if i host the server the console shows "Local" and if a client joins the server console shows "Other"?

    Is the console only showing the server side? cause the script running in the client should reach isLocalPlayer no?
     
  4. Crizz92

    Crizz92

    Joined:
    Sep 17, 2015
    Posts:
    44
    Yeah, that's it, what's run in the editor will just show what's is in the if(isLocalPlayer)

    Code (CSharp):
    1. if(isLocalPlayer)
    2. {
    3.     //will show
    4. }
    because your player is the only one local. The client that connect is not localy located on your machine so it show "other".
     
    IvanHTN likes this.
  5. IvanHTN

    IvanHTN

    Joined:
    Feb 12, 2016
    Posts:
    37
    Ah i see, i was getting a bit confused about trying different things. Thanks!