Show MY player health

Options
Hi everyone!

I'm creating a small game to help me better understand Photon before I begin creating my long-planned FPS!

I've got an issue though. I'm trying to show the players health on the screen using a Text field in Unity. new UI Unity 5.

I've successfully got the health displaying though. However, I then noticed the health I am displaying was always the players health I was shooting at.

I've attached my health.cs script which is attached to the player. Could someone tell me where I've went wrong please and why I've went wrong?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Health : MonoBehaviour
{
    public float hitPoints = 100f;
    float currentHitPoints;

    public Text healthText;

    // Use this for initialization
    void Start()
    {
        currentHitPoints = hitPoints;
        healthText = GameObject.Find("Canvas").GetComponentInChildren<Text>();
    }

    // Update is called once per frame
    void Update()
    {
        healthText.text = "Health: " + currentHitPoints;
    }

    [PunRPC]
    public void TakeDamage(float amt, string attackerName)
    {
        currentHitPoints -= amt;

        if (currentHitPoints <= 0)
        {
            Die(attackerName);
        }
    }

    void OnGUI()
    {
        if (GetComponent<PhotonView>().isMine && gameObject.tag == "Player")
        {
            if (GUI.Button(new Rect(Screen.width - 100, 0, 100, 40), "Suicide"))
            {
                Die(PhotonNetwork.player.name);
            }
        }
    }

    void Die(string attackerName)
    {
        if (GetComponent<PhotonView>().instantiationId == 0)
        {
            Destroy(gameObject);
        }
        else
        {
            if (GetComponent<PhotonView>().isMine)
            {
                if (gameObject.tag == "Player")
                { // This is my ACTUAL player, then start the respawn process.
                    new_NetworkManager nm = GameObject.FindObjectOfType<new_NetworkManager>();

                    nm.standbyCamera.enabled = true;
                    nm.respawnTimer = 5f;
                    nm.AddChatMessage(PhotonNetwork.player.name + " was killed by " + attackerName);

                }
                PhotonNetwork.Destroy(gameObject);
            }
        }
    }
}

Comments

  • Fixed.

    Changed:

    healthText.text = "Health: " + currentHitPoints;

    To:

    if(GetComponent<PhotonView>().isMine) { healthText.text = "Health: " + currentHitPoints; }