Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 winzel123 · Jul 15, 2015 at 08:54 PM · c#uitext

How can I Update HealthUI when the object is destroyed.

I have a Script called Player.cs and inside of it there is a public class called PlayerStats with a health integer.

 public class Player : Monobehaviour{
              [System.Serializable]
              public class PlayerStats
              {
                   public int Health = 100;
              }
         }
     
   

and I also have a class Health UI called HealthUI.cs with a UItext

         public class HealthUI : Monobehaviour
         {
              Text text;
              private static int health;
              private Player player
         
              void Start()
              {
              text = GetComponent <Text> ();
              player =(Player)GameObject.FindGameObjectWithTag("Player").
              GetComponent(typeof(Player));
              }
              void Update()
              {
              health = player.playerStats.Health;
              text.text = "Health" + health;
 `           `}
     }


My problem is :

1) In Scene, the UI text (Health : 100) and subtracting my health works. But when my object is destroyed and spawns again, my HealthUI doesnt reset to 100 and stops to work.

2) How can I limit my health to 0 minimum number and 100 as maximum number?

Please help :)

Comment
Add comment · Show 3
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 OctoMan · Jul 16, 2015 at 10:08 AM 0
Share
 $$anonymous$$athf.Clamp(value, $$anonymous$$, max);

This is used to clamp values between a $$anonymous$$ and a max value

When you instantiate something you need to set all values in Start() or Awake() and update the healthbar.

So make a prefab and in there put the connection to the healthbar.

avatar image InfiniBuzz · Jul 16, 2015 at 10:13 AM 0
Share

If you destroy the player object you lose its reference in your HealthUI script so you have to "refresh" the reference. Don't you get NullReferenceExceptions?

avatar image winzel123 · Jul 17, 2015 at 03:48 AM 0
Share

@InfiniBuzz Yes, I'm getting NullReferenceExceptions when I transferred my player = (Player)GameObject.FindGameObjectWithTag("Player").GetComponent(typeof(Player)); into the Update function, but my problem no.1 works now.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Burgundxyz · Jul 16, 2015 at 10:20 AM

1) Your object is deleted. So it will terminate the script there. 2)Mathf.Clamp is good... But I would have two variables. One for Max and Min and use some Boolean like if x >= max to control it. That way you can extend functionality come time if you want to add power ups to your game or something.

But Mathf.Clamp is definite good.

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
avatar image
0

Answer by NeverHopeless · Jul 16, 2015 at 10:38 AM

Regarding your problems:

my HealthUI doesnt reset to 100

This is probably because of this code:

private static int health;

Your health variable is static so it do not rely on instances. To reset health on a new instance your Start function should look like:

   void Start()
   {
      text = GetComponent <Text> ();
      player =(Player)GameObject.FindGameObjectWithTag("Player").
      GetComponent(typeof(Player));
      health = 100;  // <----------------reset the value here
   }

How can I limit my health to 0 minimum number and 100 as maximum number?

Regarding this problem you can either use Mathf.Clamp or try with Mathf.Max and Mathf.Min together, to limit the health values.

Hope it helps!

Comment
Add comment · Show 2 · 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 winzel123 · Jul 17, 2015 at 03:43 AM 0
Share

Hello, I already figured out my problem no. 1

but problem no. 2 still dont I used Health = $$anonymous$$athf.Clamp (Health, 0, 100); in my Player script , the maximum number works but the $$anonymous$$ number doesnt, I damage my player to infinity but it clamps to -100 health, not 0.

avatar image NeverHopeless · Jul 17, 2015 at 10:13 AM 0
Share

You can also go with $$anonymous$$ and max functions as i suggested.

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

6 People are following this question.

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

Related Questions

Remove the & at the end of concatenated string 0 Answers

Unity UI: Text Adventure 2 Answers

Maintaining Text Size Ratio With Screen 0 Answers

if statement not working for user input 0 Answers

Quick Question on UI Text Alignment 1 Answer


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