Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Third Party [PUN Cloud] Show MY player health

Discussion in 'Multiplayer' started by awaterpistol, Nov 9, 2015.

  1. awaterpistol

    awaterpistol

    Joined:
    Sep 3, 2015
    Posts:
    25
    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?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class Health : MonoBehaviour
    7. {
    8.     public float hitPoints = 100f;
    9.     float currentHitPoints;
    10.  
    11.     public Text healthText;
    12.  
    13.     // Use this for initialization
    14.     void Start()
    15.     {
    16.         currentHitPoints = hitPoints;
    17.         healthText = GameObject.Find("Canvas").GetComponentInChildren<Text>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         healthText.text = "Health: " + currentHitPoints;
    24.     }
    25.  
    26.     [PunRPC]
    27.     public void TakeDamage(float amt, string attackerName)
    28.     {
    29.         currentHitPoints -= amt;
    30.  
    31.         if (currentHitPoints <= 0)
    32.         {
    33.             Die(attackerName);
    34.         }
    35.     }
    36.  
    37.     void OnGUI()
    38.     {
    39.         if (GetComponent<PhotonView>().isMine && gameObject.tag == "Player")
    40.         {
    41.             if (GUI.Button(new Rect(Screen.width - 100, 0, 100, 40), "Suicide"))
    42.             {
    43.                 Die(PhotonNetwork.player.name);
    44.             }
    45.         }
    46.     }
    47.  
    48.     void Die(string attackerName)
    49.     {
    50.         if (GetComponent<PhotonView>().instantiationId == 0)
    51.         {
    52.             Destroy(gameObject);
    53.         }
    54.         else
    55.         {
    56.             if (GetComponent<PhotonView>().isMine)
    57.             {
    58.                 if (gameObject.tag == "Player")
    59.                 { // This is my ACTUAL player, then start the respawn process.
    60.                     new_NetworkManager nm = GameObject.FindObjectOfType<new_NetworkManager>();
    61.  
    62.                     nm.standbyCamera.enabled = true;
    63.                     nm.respawnTimer = 5f;
    64.                     nm.AddChatMessage(PhotonNetwork.player.name + " was killed by " + attackerName);
    65.  
    66.                 }
    67.                 PhotonNetwork.Destroy(gameObject);
    68.             }
    69.         }
    70.     }
    71. }
    72.  
     
  2. awaterpistol

    awaterpistol

    Joined:
    Sep 3, 2015
    Posts:
    25
    I fixed this issue myself.

    Thanks anyone who was going to help out. :)

    Old code:
    Code (csharp):
    1.  
    2. healthText.text = "Health: " + currentHitPoints;
    3.  
    New Code:
    Code (csharp):
    1.  
    2. if(GetComponent<PhotonView>().isMine)
    3. {
    4.     healthText.text = "Health: " + currentHitPoints;
    5. }
    6.  
     
    tobiass likes this.