Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 20, 2016 at 03:13 PM by Jordy-Rutjens for the following reason:

No response, and made progress already so I'm just trying to solve it myself.

avatar image
0
Question by Jordy-Rutjens · Sep 18, 2016 at 07:23 PM · unity 5uimultiplayerupdatehealthbar

UNET - UI Not updating with Healthbar and other bars.

Hello,

So when I run the game, the Health and such will go down in the inspector, but it wont on the UI Healthbar, and the 2 other bars( thirst and food ). Is there an easy fix for this problem? Or does it involve a full remake of this script ( Singleplayer works fine, converting to Multiplayer).

All suggestions welcome, I'd happily try them out.

Thanks for reading.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.Networking;
 
 
 public class Player_Health : NetworkBehaviour
 {
 
 
     private GameManager_Master gameManagerMaster;
     private Player_Master playerMaster;
 
     
     public float playerHealth = 100f;
 
     public Text healthText;
     public GameObject deathCanvas;
 
     public float hunger = 100f;
     public float thirst = 100f;
 
     public Image hungerBar;
     public Image thirstBar;
     public Image healthBar;
 
     public float hungerSpeedMultiplier = 0.10f;
     public float thirstSpeedMultiplier = 0.25f;
     public float playerHealthSpeedMultiplier = 0.30f;
 
 
     private bool isDying = false;
 
     public Vector3 spawnPoint = Vector3.zero;
 
 
 
 
     void OnEnable()
     {
         SetInitialReferences();
         SetUI();
         playerMaster.EventPlayerHealthDeduction += DeductHealth;
         playerMaster.EventPlayerHealthIncrease += IncreaseHealth;
 
     }
 
     void OnDisable()
     {
         playerMaster.EventPlayerHealthDeduction -= DeductHealth;
         playerMaster.EventPlayerHealthIncrease -= IncreaseHealth;
     }
 
     void Start()
     {
         StartCoroutine(TestHealthDeduction()); //deathtest
         deathCanvas.SetActive(false);
 
 
     }
 
     private void Update()
     {
         
 
         CheckDeath();
         if (hunger > 0)
         {
             hunger -= Time.deltaTime * hungerSpeedMultiplier;
         }
 
         if (thirst > 0)
         {
             thirst -= Time.deltaTime * thirstSpeedMultiplier;
         }
 
         if (hunger <= 0 || thirst <= 0 || hunger <= 0)
         {
             isDying = true;
         }
 
         else
         {
             isDying = false;
         }
 
         if (isDying == true)
         {
             playerHealth -= Time.deltaTime * playerHealthSpeedMultiplier;
         }
 
         if(hunger > 100)
         {
             hunger = 100f;
         }
 
         if (thirst > 100)
         {
             thirst = 100f;
         }
 
 
         
 
         healthBar.fillAmount = playerHealth / 100;
         hungerBar.fillAmount = hunger / 100;
         thirstBar.fillAmount = thirst / 100;
        
     }
 
     private void CheckDeath()
     {
         if (playerHealth <= 0)
         {
             Die();
         }
 
     }
 
     void SetInitialReferences()
     {
         gameManagerMaster = GameObject.Find("Game.Manager").GetComponent<GameManager_Master>();
         playerMaster = GetComponent<Player_Master>();
     }
 
 
 
 
     IEnumerator TestHealthDeduction()
     {
         yield return new WaitForSeconds(2);
         DeductHealth(20); // Removes health *@**@*@*@**@*@*@*@
 
         yield return new WaitForSeconds(4);
         DeductHealth(20); // Removes health *@**@*@*@**@*@*@*@
     }
 
 
     void DeductHealth(int healthChange)
     {
         playerHealth -= healthChange;
 
         if(playerHealth <=0)
         {
             playerHealth = 0;
             Die();
         }
 
         SetUI();
     }
 
     private void Die()
     {
         deathCanvas.SetActive(true);
         gameObject.SetActive(false);
     }
 
     public void Respawn()
     {
         playerHealth = 100;
         hunger = 100;
         thirst = 100;
         deathCanvas.SetActive(false);
         transform.position = spawnPoint;
         gameObject.SetActive(true);
     }
 
     public void AddHunger(float amount)
     {
         hunger += amount;
     }
 
     public void AddThirst(float amount)
     {
         thirst += amount;
     }
 
 
 
 
     void IncreaseHealth(int healthChange)
     {
         playerHealth += healthChange;
 
         if(playerHealth > 100)
         {
             playerHealth = 100;
         }
 
 
         SetUI();
     }
 
     void SetUI()
     {
         if(healthText !=null)
         {
             healthText.text = playerHealth.ToString();
         }
 
     }
 
 
 
 
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image subzer0 · Sep 25, 2016 at 06:32 PM 0
Share

I am having the same problem. I am making a shooter game, and every player has a health bar above him.

When a player shoots at an enemy, the fillAmount of the health bar of the enemy decreases successfully, but it is not updating in the enemy's window.

Have you found a solution ?

I know you closed the question, but I really need help. Thanks.

1 Reply

  • Sort: 
avatar image
0

Answer by SergioSandiaz · Sep 19, 2016 at 06:00 AM

Why don´t you use a slider?

 slider.value = playerHealth;

I see your using images and I don´t understand why

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jordy-Rutjens · Sep 19, 2016 at 09:38 AM 0
Share

Image uses fill amount, so it reduces in size when the int does.

The image and ints just don't update with the UI and the health ints.

I think it has to do with the networking, any clue?

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiplayer Health Bar 1 Answer

multiply Score on Collision not working? check my code plz 2 Answers

Destroying UI elements problem 1 Answer

im making a multiplayer game, and it has buttons that allow you to craft different blocks, how do I set a reference to the player game object so each player can build different amounts of blocks? 0 Answers

Pressing two on-screen buttons at the same time, with the same touch - Android Unity Event-Triggers 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges