Search Unity

Player HUD only visible to Instantiated Player

Discussion in 'Multiplayer' started by Jozzuph, Feb 13, 2023.

  1. Jozzuph

    Jozzuph

    Joined:
    Sep 12, 2019
    Posts:
    33
    Hey, so I'm trying to make a multiplayer game with Netcode for Game Objects, and I can't figure out how to get the player HUD to only connect for the owner player. Currently, they all overlap each other.

    I have the hierarchy set up like this.

    HUD.PNG

    I would appreciate some beginner friendly advice, please!


    I also have another problem when an entity gets damaged and is supposed to blink red, but it only displays this correctly for the Client, and all clients instantiated before the n'th client.

    i.e. if there are five clients, client #1 can only see itself blink red, whereas client #4 can see clients 1-4 blink red.
     
    Last edited: Feb 13, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,990
    Make sure the UI, none of it, is dealing in any way with the network. Do not try to sync the UI with the network. Then it should become easy: when a player spawns (onclientconnect or onnetworkspawn) and the IsLocalPlayer flag is true, that‘s when you spawn or show the UI.

    Any network variable such as player health that gets displayed in the UI is updated by sending events from where that NetworkVariable is that the UI listens to.

    For more info research general best practices for decoupling UI from game logic.
     
    Kinbury and Jozzuph like this.
  3. Jozzuph

    Jozzuph

    Joined:
    Sep 12, 2019
    Posts:
    33
    Thanks for the quick response. By showing the UI do you mean I should originally have the object disabled. Then when the player connects OnNetworkSpawn() I enable the UI?

    Also what is the difference in this case between IsOwner, IsClient, and IsLocalPlayer?
     
    Last edited: Feb 13, 2023
  4. Jozzuph

    Jozzuph

    Joined:
    Sep 12, 2019
    Posts:
    33
    Hmm... This doesn't seem to be working

    In my player script I find the HUD.
    Code (CSharp):
    1. public override void OnNetworkSpawn()
    2.     {
    3.         base.OnNetworkSpawn();
    4.  
    5.         if (!IsLocalPlayer || !IsOwner) return;
    6.         hud = transform.Find("Canvas Player HUD").gameObject;
    7.         hud.SetActive(true);
    8.     }
    My HUD script has nothing to do with the network but gets a connection to the player on start

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System;
    4. using TMPro;
    5.  
    6. public class HUDManager : MonoBehaviour
    7. {
    8.     private EntityStats entityStats;
    9.  
    10.     public void Start()
    11.     {
    12.         entityStats = transform.root.GetComponent<EntityStats>();
    13.  
    14.         healthBar.maxValue = entityStats._maxHealth.Value;
    15.         manaBar.maxValue = entityStats._maxMana.Value;
    16.  
    17.         healthBar.value = entityStats._currentHealth.Value;
    18.         manaBar.value = entityStats._currentMana.Value;
    19.     }
    20. }
     
  5. Jozzuph

    Jozzuph

    Joined:
    Sep 12, 2019
    Posts:
    33

    Nevermind! This all works, I forgot I kept the HUD enabled because I was editing it... Thanks for the help!

    One more question, though, can you give me an example of when I should use
    IsLocalPlayer vs IsOwner vs IsClient?