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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

NullReferenceException

Discussion in 'Scripting' started by canadienwolf, Oct 4, 2018.

  1. canadienwolf

    canadienwolf

    Joined:
    Oct 4, 2018
    Posts:
    4
    NullReferenceException: Object reference not set to an instance of an object
    PlayerHealthManager.HurtPlayer (Int32 damageToGive) (at Assets/Scripts/Player/PlayerHealthManager.cs:91)
    HurtPlayer.OnCollisionEnter2D (UnityEngine.Collision2D other) (at Assets/Scripts/Enemy/HurtPlayer.cs:22)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class PlayerHealthManager : MonoBehaviour {
    8.  
    9.     // Options that we will be able to change in Unity with sliders:
    10.     public int startingHealth;                                      
    11.     public int playerCurrentHealth;                                        
    12.     public Slider healthSlider;                                                
    13.     public Image damageImage;                                                
    14.     public AudioClip deathClip;                                                
    15.     public float flashspeed = 5f;                                          
    16.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);                
    17.  
    18.     Animator anim;                                                          
    19.     AudioSource playerAudio;                                              
    20.     PlayerMovement PlayerMovement;                                            
    21.     bool isDead;                                                          
    22.     bool damaged;                                                          
    23.     [HideInInspector] public bool knockedBack = false;                        
    24.     bool knockedUp = false;
    25.     bool knockTowardsRight;
    26.     [Header ("Knockback")]
    27.     public float knockbackForce = 8.0f;
    28.     public float knockupForce = 5.0f;
    29.     public float knockbackTime = 0.4f;
    30.  
    31.     void Awake ()
    32.     {
    33.         anim = GetComponent <Animator> ();
    34.         playerAudio = GetComponent <AudioSource>();
    35.         PlayerMovement = GetComponent <PlayerMovement> ();
    36.        
    37.     }
    38.  
    39.     // Use this for initialization
    40.     void Start () {
    41.         playerCurrentHealth = startingHealth;  
    42.     }
    43.    
    44.     // Update is called once per frame
    45.     void Update () {
    46.         if(playerCurrentHealth < 0)
    47.         {
    48.             // gameObject.SetActive(false);
    49.  
    50.             // Loads current scene if player dies
    51.               SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);
    52.            
    53.  
    54.  
    55.         }
    56.  
    57.         // Tells the game which way the characters should be thrown when hit by an enemy.
    58.         if (knockedBack)
    59.         {
    60.             if (knockTowardsRight)
    61.             {
    62.                 transform.position -= new Vector3 (knockbackForce * Time.deltaTime, 0, 0);
    63.             }
    64.             else
    65.             {
    66.                 transform.position += new Vector3 (knockbackForce * Time.deltaTime, 0, 0);
    67.             }
    68.            
    69.             if (knockedUp)
    70.             {
    71.                 transform.position += new Vector3 (0, knockupForce * Time.deltaTime, 0);
    72.             }
    73.             else if (!GetComponent <PlayerMovement> ().controller.m_Grounded)
    74.             {
    75.                 transform.position -= new Vector3 (0, knockupForce * Time.deltaTime, 0);
    76.             }
    77.         }
    78.     }
    79.  
    80.  
    81.     // Makes it possible to decide how much health the player should have
    82.     public void HurtPlayer(int damageToGive)
    83.         {
    84.         // Set the damaged flag so the screen will flash.
    85.         damaged = true;
    86.  
    87.         // Reduce the current health by the damage amount.
    88.         playerCurrentHealth -= damageToGive;
    89.  
    90.         // Set the health bar's value to the current health.
    91.         healthSlider.value = playerCurrentHealth;
    92.  
    93.         // Play the hurt sound effect.
    94.         playerAudio.Play ();
    95.  
    96.         // If the player has lost all it's health and the death flag hasn't been set yet...
    97.         if (playerCurrentHealth <= 0 && !isDead);
    98.        
    99.         // ... it should die.
    100.         Death ();
    101.            
    102.         }
    103.  
    104.         void Death ()
    105.         {
    106.             // Set the death flag so this function won't be called again.
    107.             isDead = true;
    108.  
    109.             // Tell the animator that the player is dead.
    110.             anim.SetTrigger ("Die");
    111.  
    112.             // Set the audiosource to play the death clip and play it (this will stop the hurt sound from playing).
    113.             playerAudio.clip = deathClip;
    114.             playerAudio.Play ();
    115.  
    116.             // Turn off the movement and shooting scripts.
    117.                PlayerMovement.enabled = false;
    118.         }
    119.  
    120.     //
    121.     void OnCollisionEnter2D (Collision2D col)
    122.     {
    123.         if (col.gameObject.tag    == "AI")
    124.             {
    125.                 knockedBack = true;
    126.                 knockedUp = true;
    127.  
    128.                 if (col.transform.position.x > transform.position.x)
    129.                 {
    130.                     knockTowardsRight = true;
    131.                 }
    132.                 else
    133.                 {
    134.                     knockTowardsRight = false;
    135.                 }
    136.  
    137.                 Invoke ("StopKnockback", knockbackTime);
    138.                 Invoke ("StopKnockup", knockbackTime / 2);
    139.             }
    140.     }
    141.  
    142.     void StopKnockback ()
    143.     {
    144.         knockedBack = false;
    145.     }
    146.  
    147.         void StopKnockup ()
    148.     {
    149.         knockedUp = false;
    150.     }
    151. }
    152.  
    153.     // // If the player has just been damaged...
    154.     // if(damaged)
    155.     // {
    156.     //     // ... set the colour of the damageImage to the flash colour.
    157.     //     damageImage.color = flashColour;
    158.     // }
    159.     // // Otherwise...
    160.     // else
    161.     // {
    162.     //      // ... transition the colour back to clear.
    163.     //     damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashspeed * Time.deltaTime);
    164.     // }
    165.     // // Reset the damaged flag.
    166.     // damaged = false;
    167.  
     
  2. canadienwolf

    canadienwolf

    Joined:
    Oct 4, 2018
    Posts:
    4
    I am trying to find the solution, but nothing is showing up when I open the script. Have also tried using different links within the scripts and naming stuff differently but this one persists.
     
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    You probably forgot to link healthSlider in your scene.
     
    canadienwolf likes this.