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 /
avatar image
0
Question by Nosmo · Mar 22, 2018 at 06:16 PM · addhealthbarincreaseplayer damagehealth

how to make a healthbar increase

I'm trying to add to an existing script a way to increase health by 10 points, the health pickup is destroyed so thats no problem. Im getting a CS0019 error can anyone help?

 public PlayerHealth playerHealth;
 public Slider healthSlider; 
 public float HealthUp = 10;
 //PlayerHealth.currenntHealth playerHealth.currentHealth;
     
 void OnTriggerEnter (Collider col) 
 {
     if (col.gameObject.tag == "Player")

     {
         Destroy(gameObject);
         Debug.Log ("pickup working");
     }
 }

 private void Update ()
 {
     if (playerHealth < 100)

         playerHealth = playerHealth + HealthUp;
 }
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 Nosmo · Mar 26, 2018 at 05:05 PM 0
Share

new health script is:

public class HealthPickUp : $$anonymous$$onoBehaviour {

 public float playerHealth = 100;
 public Slider healthSlider; 
 public float HealthUp = 10;

     
 void OnTriggerEnter (Collider col) 
 {
     if (col.gameObject.tag == "Player")

     {
     Destroy(gameObject);
     Debug.Log ("1st step working");
     }
 }

 void Update ()
 {
     if (playerHealth < 100)
     {        
         RestoreHealthSlider ();
     }
 }

 public void RestoreHealthSlider()
 {
     healthSlider.value = playerHealth + HealthUp;
 }

 private void debug()
 {
     Debug.Log ("2nd step working");    
 }

}

The pickup is working and there are no errors but the health still isn't being restored, any suggestions?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cornelis-de-Jager · Mar 22, 2018 at 11:17 PM

The issue is that you are trying to compare to different types int vs playerHealth.

  if (playerHealth < 100) // <---- PlayerHealth vs Integer
          playerHealth = playerHealth + HealthUp; // <---- PlayerHealth vs float

You either need to change player health to a float or add a health component to the PlayerHealth class that is a float

Example 1:

 Public float playerHealth = 100;
 ...
 if (playerHealth < 100)
     playerHealth += HealthUp;

Example 2:

 if (playerHealth.health < 100)
    playerHealth.health += HealthUp;

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 Topthink · Mar 23, 2018 at 12:22 AM

If I understand your question correctly, you may wish to use "Invoke Repeat"

Thus, as you suggest in your question, you would have some sort of test to see if your health is below a certain amount (playerHealth < maxHealth) etc. If so, you would do something like this:

 void Start()
     {
         InvokeRepeating("HealthIncrease", 5.0f, 1.5f);
     }


     void HealthIncrease()
     {
         Health = Health + addSomeNewHealth;
     }
 

So, the function "HealthIncrease" would be called. After 5.0 seconds it would begin. Then every 1.5 seconds it would increase your health by the "addSomeNewHealth" amount. Of course, you would use whatever time and increase amounts were appropriate.

Good luck and I'm sorry if this isn't what you were asking.

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 Nosmo · Mar 28, 2018 at 02:24 PM 0
Share

Sorry but it didnt work - I have added my current script. If you have any suggestions I'd love to hear them.

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

77 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

Related Questions

(4.6 UI) Canvas position resetting itself to (0,0,0) when instantiating a new prefab with the canvas as child? 2 Answers

What's wrong with this health bar GUI script? (C#) 1 Answer

Semicircular Health/Mana/Progress Bars 0 Answers

Health below zero but shows negative numbers 1 Answer

health bar 2 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