Search Unity

Question Help with Restoring Health

Discussion in 'Scripting' started by NerdAlex, Aug 5, 2020.

  1. NerdAlex

    NerdAlex

    Joined:
    Apr 23, 2017
    Posts:
    12
    I'm trying to implement a checkpoint system in my game, where when a player clicks on the checkpoint, they will have their health refilled to full. However, whenever I try to test that part of the code, it gives me a Null Reference Exception and the HealthSlider becomes null. It's assigned in the inspector so I don't know what's causing this. Every other part of the code seems to be working.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class PlayerHealth : MonoBehaviour
    8. {
    9.  
    10.     public int startHealth = 2;
    11.  
    12.     public int currentHealth;
    13.  
    14.     public Slider HealthSlider;
    15.  
    16.     public AudioClip hurtClip;
    17.     public AudioClip deathClip;
    18.  
    19.     public Animator anim;
    20.  
    21.     public AudioSource playerAudio;
    22.  
    23.     public PlayerMovement pm;
    24.  
    25.     bool isDead;
    26.  
    27.     bool damaged;
    28.  
    29.  
    30.     // Start is called before the first frame update
    31.     void Awake()
    32.     {
    33.         anim = GetComponent<Animator>();
    34.         playerAudio = GetComponent<AudioSource>();
    35.         pm = GetComponent<PlayerMovement>();
    36.  
    37.         currentHealth = startHealth;
    38.  
    39.     }
    40.  
    41.     // Update is called once per frame
    42.     void Update()
    43.     {
    44.         if (damaged)
    45.         {
    46.             playerAudio.clip = hurtClip;
    47.             playerAudio.Play();
    48.             damaged = false;
    49.         }
    50.  
    51.         HealthSlider.value = currentHealth;
    52.     }
    53.  
    54.     public void TakeDamage(int amount)
    55.     {
    56.         damaged = true;
    57.  
    58.         currentHealth -= amount;
    59.  
    60.         if (currentHealth <= 0 && !isDead)
    61.         {
    62.             Death();
    63.         }
    64.     }
    65.  
    66.     void Death()
    67.     {
    68.         isDead = true;
    69.  
    70.         anim.SetTrigger("Die");
    71.  
    72.         playerAudio.clip = deathClip;
    73.         playerAudio.Play();
    74.  
    75.         pm.enabled = false;
    76.     }
    77.  
    78.     public void ResetHealth()
    79.     {
    80.         currentHealth = startHealth;
    81.         print(HealthSlider);
    82.         HealthSlider.value = currentHealth;
    83.     }
    84. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Are both the PlayerHealth object and the HealthSlider object in the scene? Or is one of them a prefab, or Instantiated from a prefab?
     
  3. NerdAlex

    NerdAlex

    Joined:
    Apr 23, 2017
    Posts:
    12
    Both of them are in the scene as objects.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    How do you call ResetHealth? From a UI button? From another script?
     
  5. NerdAlex

    NerdAlex

    Joined:
    Apr 23, 2017
    Posts:
    12
    From an OnExecute() Event
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    My question is basically, where are you getting the PlayerHealth reference from to call ResetHealth?
     
  7. NerdAlex

    NerdAlex

    Joined:
    Apr 23, 2017
    Posts:
    12
    It's in the same script
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Where? I don't see it above.
     
  9. NerdAlex

    NerdAlex

    Joined:
    Apr 23, 2017
    Posts:
    12
    I'm confused. Are you asking what I'm doing to call the method to run?
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
  11. NerdAlex

    NerdAlex

    Joined:
    Apr 23, 2017
    Posts:
    12
    upload_2020-8-5_17-37-16.png

    I'm using Pixel Crushers' Dialogue System to call it when I click on the checkpoint. The alert is showing up so the trigger is activating.
     
  12. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    So that John object from the screenshot. Is that your player from the scene, or is it a prefab?
     
  13. NerdAlex

    NerdAlex

    Joined:
    Apr 23, 2017
    Posts:
    12
    It's from a prefab.
     
  14. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    So if it's referring to the prefab, that's not your player instance that's in the scene at runtime, right?
     
  15. NerdAlex

    NerdAlex

    Joined:
    Apr 23, 2017
    Posts:
    12
    I guess not. It's instantiated from the prefab, though.