- Home /
Healthpickup isn't working
I've written a script for restoring health and it works on the player in the prefab folder but not with the player in the gaming area
code as it is:
public PlayerHealth playerHealth;
public Slider healthSlider;
public int HealthUp = 10;
void OnTriggerEnter(Collider col)
{
if (col.tag == "Player")
{
playerHealth.currentHealth += HealthUp; // x = pHc + 10
healthSlider.value += playerHealth.currentHealth; //x = hS + pHc
Debug.Log ("Stage 1; Health restored");
gameObject.SetActive(false);
Debug.Log ("stage 2: Health pick gone");
}
}
The references are all correct, the pickup disappears but no health restore. Any suggestions?
Answer by Priyanka-Rajwanshi · Apr 06, 2018 at 01:55 PM
@Nosmo Have you dragged a prefab as reference for PlayerHealth and slider?
The dragged reference of playerHealth and healthSlider variable should be present in the game scene.
If your healthpickup is instantiated, add references to these variables after instantiation.
I had to make a prefab of the slider and player but I've referenced them both
the codes been altered and now the health is restored but the slider remains where is it after the player got injured:
public void RestoreSlider()
{
healthSlider += healthPickUp;
healthSlider.value = currentHealth;
}
i get the following message from the above code:
error CS0019: Operator +=' cannot be applied to operands of type
UnityEngine.UI.Slider' and `HealthPickUp'
This has happened to me before but i can't remember how to fix it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612160211im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Regain health on GUI 2 Answers
How to set up a rhomboid health bar? 2 Answers
I need help with the player's health points 1 Answer
Player Heal *Help* 0 Answers
How to make a camera's background be another camera's veiw 1 Answer