Help with health in UI.

Hello
I am in the process of making a multiplayer fps, but have come upon a problem, when showing the health for the local player on the UI. The health is working behind the scenes, but when it's shown on the UI, both the player who shoots and the player who gets shot loses health. (again only on the UI side). Is there any way to stop it from changing on the shooting player ?

Thanks in advance.

Health.cs

[code2=csharp]using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ExitGames.Client.Photon;
using Hashtable = ExitGames.Client.Photon.Hashtable;


public class Health : Photon.MonoBehaviour {

public float hitPoints = 100;
public float currentHitPoints;

// Use this for initialization
void Start () {
currentHitPoints = hitPoints;
FindObjectOfType<guiScript>().guiHealth = currentHitPoints;
}

[RPC]
public void TakeDamage(float amt, int shooterID, string shooterName) {
FindObjectOfType<guiScript>().takeGUIDamage(currentHitPoints);
currentHitPoints -= amt;

if (currentHitPoints <= 0) {
if(photonView.isMine) {
Hashtable props = PhotonNetwork.player.customProperties;
props["Deaths"] = (int)props["Deaths"] + 1;
PhotonNetwork.SetPlayerCustomProperties(props);
}

Debug.Log(shooterName);
foreach(PhotonPlayer player in PhotonNetwork.playerList) {
if(player.name == shooterName) {
Debug.Log("Roev");
Hashtable old_props = player.customProperties;
Hashtable props = new Hashtable();
props["Kills"] = (int)old_props["Kills"] + 1;
player.SetCustomProperties( props );

}

}

Die();
if (GetComponent<PhotonView>().isMine) {
guiScript gs = FindObjectOfType<guiScript>();
gs.isRespawning = true;
}
}
}





void Die() {
if (GetComponent<PhotonView> ().instantiationId == 0) {
Destroy (gameObject);
} else {
if (GetComponent<PhotonView>().isMine) {

if (gameObject.tag == "Player") {
networkManager nm = GameObject.FindObjectOfType<networkManager>();
nm.standbyCamera.SetActive(true);
nm.respawnTimer = 3f;
}

PhotonNetwork.RemoveRPCs(targetPlayer:PhotonNetwork.player);
PhotonNetwork.Destroy (gameObject);

}
}
}

}[/code2]

GUIScript.cs
[code2=csharp]using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using ExitGames.Client.Photon;
using Hashtable = ExitGames.Client.Photon.Hashtable;


public class guiScript : Photon.MonoBehaviour {

public bool isRespawning;
public GameObject respawn;
public float guiHealth;

public void OnGUI() {

GetComponentInChildren<Text>().text = guiHealth + " HP";

}


[RPC]
public void takeGUIDamage (float health) {
guiHealth = health;
Debug.Log(guiHealth);

}

// Update is called once per frame
void Update () {
if (isRespawning) {
respawn.SetActive(true);
networkManager nm = GameObject.FindObjectOfType<networkManager>();
respawn.GetComponentInChildren<Text>().text = "" + nm.respawnTimer.ToString("F1");

} else {
respawn.SetActive(false);
}

}
}[/code2]

Comments

  • Hi,

    The guiScript object is network-synchronized with RPC. That makes it look same on all clients. Synchronizing ui hardly makes sense since clients should have different uis with data only relevant to local client. Make ui plain object and update it if local player state changed.