Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved My Healing script wont work

Discussion in 'Scripting' started by ShamasterAlpha3, Sep 11, 2020.

  1. ShamasterAlpha3

    ShamasterAlpha3

    Joined:
    Aug 5, 2020
    Posts:
    8
    I am making a top down rpg, I have a health bar slider. The damage script works so my player can take damage. but when I pick up an item, im supposed to click it on the HUD to heal some hp. I have no errors in the health button script. I will provide all the scripts below.
    Healing Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HealthButton : MonoBehaviour
    6. {
    7.     public float heal;
    8.     public PlayerHealth playerhealth;
    9.     public HealthBar healthBar;
    10.     public GameObject effect;
    11.     private Transform player;
    12.  
    13.  
    14.     private void Start()
    15.     {
    16.         player = GameObject.FindGameObjectWithTag("Player").transform;
    17.     }
    18.  
    19.     public void Use()
    20.     {
    21.         HealPlayer(10);
    22.         Instantiate(effect, player.position, Quaternion.identity);
    23.         Destroy(gameObject);
    24.        
    25.     }
    26.  
    27.     public void HealPlayer(int heal)
    28.     {
    29.  
    30.  
    31.         playerhealth.currentHealth += heal;
    32.  
    33.         healthBar.slider.value = playerhealth.currentHealth;
    34.     }
    35. }
    36.  
    player health script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerHealth : MonoBehaviour
    6. {
    7.     public int maxHealth = 100;
    8.     public int currentHealth;
    9.  
    10.     void Start()
    11.     {
    12.         currentHealth = maxHealth;
    13.     }
    14.  
    15.  
    16.     void Update()
    17.     {
    18.        
    19.     }
    20. }
    21.  
    Health bar script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HealthBar : MonoBehaviour
    7. {
    8.     public Slider slider;
    9.  
    10.     public void SetMaxHealth(int health)
    11.     {
    12.         slider.maxValue = health;
    13.         slider.value = health;
    14.     }
    15.     public void SetHealth(int health)
    16.     {
    17.         slider.value = health;
    18.     }
    19. }
    20.  
    damage player script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerDamaged : MonoBehaviour
    6. {
    7.     [SerializeField] private float damage;
    8.     [SerializeField] private PlayerHealth playerhealth;
    9.     [SerializeField] private HealthBar healthBar;
    10.  
    11.     private void OnTriggerEnter2D(Collider2D Collision)
    12.     {
    13.         if (Collision.CompareTag("Player"))
    14.         {
    15.             Damage(10);
    16.         }
    17.     }
    18.  
    19.     void Damage(int damage)
    20.     {
    21.  
    22.  
    23.         playerhealth.currentHealth -= damage;
    24.  
    25.         healthBar.slider.value = playerhealth.currentHealth;
    26.     }
    27.  
    28.  
    29.  
    30.  
    31.  
    32. }

    all help is appreciated
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    Zer0Cool likes this.
  3. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    I assume your classes "HealthButton" and "PlayerDamaged" has no connection to each other and therefore the "playerhealth" is not connected too.
    So if you change the "playerhealth.currentHealth" of the class "PlayerDamaged" this change doesnt reflect in the "playerhealth.currentHealth" of the class "HealthButton".

    Or the problem could be that the referenes are wrong set... So the "HealthButton" sits on another gameObject reference (for the PlayerHealth class) than the"PlayerDamaged" class

    On way to solve this you could create an empty gameObject in your scene and attach the PlayerHealth class there. This way the gameObject would store the value of "playerhealth.currentHealth" and so you have a more clear separation of the class instances in your game.
    Then you pass the reference to this gameObject to your "HealthButton" and "PlayerDamaged" classes. But you should think about what gameObjects in your scene are holding instances of these 2 classes too...

    Example of this implementation for the "HealthButton" class (you have to do the same for the "PlayerDamaged" class):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class HealthButton : MonoBehaviour
    5. {
    6.     public float heal;
    7.     public GameObject playerhealthGO; // this GO is holding the instance of the player's health
    8.     public HealthBar healthBar;
    9.     public GameObject effect;
    10.  
    11.     private Transform player;
    12.     private PlayerHealth playerhealth;
    13.  
    14.     private void Start()
    15.     {
    16.         player = GameObject.FindGameObjectWithTag("Player").transform;
    17.         playerhealth = playerhealthGO.GetComponent<PlayerHealth>();
    18.     }
    19.     public void Use()
    20.     {
    21.         HealPlayer(10);
    22.         Instantiate(effect, player.position, Quaternion.identity);
    23.         Destroy(gameObject);
    24.  
    25.     }
    26.     public void HealPlayer(int heal)
    27.     {
    28.         playerhealth.currentHealth += heal;
    29.         healthBar.slider.value = playerhealth.currentHealth;
    30.     }
    31. }
     
    Last edited: Sep 11, 2020