Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by AmateurPrograming · Jan 18, 2021 at 05:37 PM · integerhealth

My integer for my player health seems to be only returned once

I can't get my health sprite to change when my player health is at a certain level. I am really not sure how to fix it or explain what is going on. When I run my game, it shows on the console that my health is full. However when I take one damage it doesn't show that my health has lowered.

Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System;
 
 public class HealthSystem
 {
     public int health;
     public int maxHealth;
     public Image hpImage;
     private Sprite hp;
 
     public HealthSystem(int maxHealth)
     {
         this.maxHealth = maxHealth;
         health = maxHealth;
     }
     public int GetHealth()
     {
         return health;
     }
 
     public void Damage(int damageAmount)
     {
         health -= damageAmount;
         if (health < 0)
         {
             health = 0;
         }
     }
     public void Heal(int healAmount)
     {
         health += healAmount;
         if (health > maxHealth)
         {
             health = maxHealth;
         }
     }
     
 }
 ______________________________________________________________________________________________________
 public class Player_UI : MonoBehaviour
 {
     private bool plusOneDmg;
     private bool healOneHeart;
     private bool padHit = false;
     public Image healthImg;
 
 
     HealthSystem healthSystem = new HealthSystem(4);
 
     private void Start()
     {
         
 
         Debug.Log("Health:" + healthSystem.GetHealth());
 
 
     }
     private void Update()
     {
     
     }
 
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.tag == "Lava" && padHit == false)
         {
             healthSystem.Damage(1);
             Debug.Log("Health:" + healthSystem.GetHealth());
             padHit = true;
         }
         else if (collision.gameObject.tag == "HealCube" && padHit == false)
         {
             healthSystem.Heal(1);
             Debug.Log("Health:" + healthSystem.GetHealth());
             padHit = true;
         }
     }
     private void OnCollisionExit2D(Collision2D collision)
     {
         if (collision.gameObject.tag == "Lava" && padHit == true)
         {
             padHit = false;
         }
         else if (collision.gameObject.tag == "HealCube" && padHit == true)
         {
             padHit = false;
         }
     }
 }


 [RequireComponent(typeof(Image))]
 public class HealthBar : MonoBehaviour
 {
     public Sprite Hp4, Hp3, Hp2, Hp1, Hp0;
     public Image image;
 
     HealthSystem healthSystem = new HealthSystem(4);
     private void Start()
     {
 
         hpBar();
 
         image = gameObject.GetComponent<Image>();
 
 
     }
     private void Update()
     {
         
     }
     private void hpBar()
     {
 
 
         if (healthSystem.health == healthSystem.maxHealth)
         {
             Debug.Log("FullHp");
             image.sprite = Hp4;
         }
         if (healthSystem.health == 3)
         {
             Debug.Log("LessHp");
             image.sprite = Hp3;
         }
         if (healthSystem.health == 2)
         {
             Debug.Log("LesserHp");
             image.sprite = Hp2;
         }
         if (healthSystem.health == 1)
         {
             Debug.Log("EvenLesserHp");
             image.sprite = Hp1;
         }
         if (healthSystem.health == 0)
         {
             Debug.Log("NoHp");
             image.sprite = Hp0;
         }
     }
 }
 
 







Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jackmw94 · Jan 18, 2021 at 06:00 PM

Okay, I think this issue is a couple of issues:

First of all, if you point at the code where you're performing the behaviour you expect to happen and walk backwards through it to all the points at which it could be called, you'll see that it's only ever called on start and therefore won't happen at all after the game is up and running. It seems as though this behaviour happens in your "hpBar" function within HealthBar - this is only ever called in Start.

However before we discuss where else to update this value, I've noticed that both HealthBar and Player_UI each own their own HealthSystem instance. When you call new HealthSystem(4) you are creating a brand new instance of the HealthSystem class, so given that you call it once in HealthBar and again in Player_UI, you'll have 2 separate objects that you use at different points.

So my advice to you is to:

Get rid of the HealthSystem within HealthBar, use only the one within Player_UI. Make that hpBar into "UpdateHpBar" or something and have it as public with an argument for the health value, it can then be updated whenever you change the actual health.

Also, for your own sanity, you might want to remove the Image and Sprite from HealthSystem given they're not the images or sprites you're using for health. If you had plans for them then ignore me! :)

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 AmateurPrograming · Jan 20, 2021 at 05:38 PM 1
Share

Sorry if I am a bit late, thanks for your suggestion. It works now!

The Image and Sprite from HealthSystem was when I was trying to have the health change from that class. So yeah, they are redundant. Thanks.

avatar image
0

Answer by bdubbert · Jan 18, 2021 at 05:49 PM

You need to call HPBar() somewhere, either in update or when your health changes. Right now you only call it in start.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

newbie question how would get the percentage of its original starting value over its current value 1 Answer

Calling a variable from another class 2 Answers

Making a hunger script 1 Answer

SendMessage is working but the variable is not being updated on the other end 1 Answer

Semicircular Health/Mana/Progress Bars 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