- Home /
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;
}
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?
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;
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.
Sorry but it didnt work - I have added my current script. If you have any suggestions I'd love to hear them.
Your answer
Follow this Question
Related Questions
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