Healthbar in a Multiplayer game
I've been trying to get help for the last two weeks to no avail. My game is a multiplayer fps and I have the player's health bar in the top left corner of the screen. It works perfectly when playing alone, but once another player joins the bars overlap, making it very hard to read what is going on. The line of code I am using is:
void OnGUI(){ if (GetComponent().isMine) { GUI.Box (new Rect (10, 10, 100, 30), "HP | " + currentHealth); } }
Why doesn't this work and how can I fix this? Thanks!
Edit: It doesn't show for whatever reason in my post, but I am using the Network view component.
@Noouhh when I made my healthbars for my multiplayer game I made them regular gameobjects (Unity UI objects really). When you start your game the player could find the Healthbar by GameObject.Find("Healthbar") and set it as his/her healthbar (and update it accordingly). Since the Healthbar doesn't have a NetworkIdentity it means that the healthbar is client-sided only. Thus the gameobject will only be available to the client and not be seen on other clients.
If you for some reason need to have the healthbar as a child of the player (as in the case of a healthbar above the head in a $$anonymous$$$$anonymous$$ORPG) you can use if(!isLocalPlayer) {Healthbar.SetActive(false)
. This will disable the healthbar for everyone except the local player!
Your answer
Follow this Question
Related Questions
[Help!] Showing only your health in a Multiplayer FPS game 0 Answers
Can't find StartHost() function for a button 1 Answer
How to make GUI text only visible on that player's screen (and no one elses) in multiplayer? 1 Answer
How to make a health script for a custom health bar (In A Canvas)? 3 Answers
Help with multiplayer FPS health 1 Answer