How to sync health bar with Photon Unity Network
I'm working on a multiplayer FPS game. The health bar is a slider. The problem is that the debug logs show me that the value of the slider and the health of the player are working properly when he is taking damage, but the slider itself is not decreasing. (I'm using another class for the shooting specs.). Another problem is that when the player is taking some damage and you exit the game and come back in, the healthbar.value is not initialized to 100 as it should be.... So the slider is about, let's say 50%, and you exit the game, when you come back into the room, you have 100% actual health but 50% slider health. I'm a bit confused, any suggestions? Here is the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Health : Photon.MonoBehaviour
{
public int maxHP;
int currentHP;
[SerializeField]
private Slider healthBar;
void Start()
{
maxHP = 100;
currentHP = maxHP;
healthBar.value = maxHP;
}
[PunRPC]
public void TakeDamage(int damage)
{
currentHP -= damage;
Debug.Log("player: "+currentHP);
healthBar.value = currentHP;
Debug.Log("healthbar: " + healthBar.value);
if (currentHP <= 0)
{
Die();
healthBar.value = 0;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612083635im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Photon synchronization on different devices failes 0 Answers
Healthbar/UI Slider glow effect 1 Answer
Photon synchronization on different devices failes 0 Answers
problems with slider bar,Problem with slider bar 0 Answers
How do i get the two client's units to match up rather than be incorrect units? 1 Answer