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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class PlayerHealthManager : MonoBehaviour { // Options that we will be able to change in Unity with sliders: public int startingHealth; public int playerCurrentHealth; public Slider healthSlider; public Image damageImage; public AudioClip deathClip; public float flashspeed = 5f; public Color flashColour = new Color(1f, 0f, 0f, 0.1f); Animator anim; AudioSource playerAudio; PlayerMovement PlayerMovement; bool isDead; bool damaged; [HideInInspector] public bool knockedBack = false; bool knockedUp = false; bool knockTowardsRight; [Header ("Knockback")] public float knockbackForce = 8.0f; public float knockupForce = 5.0f; public float knockbackTime = 0.4f; void Awake () { anim = GetComponent <Animator> (); playerAudio = GetComponent <AudioSource>(); PlayerMovement = GetComponent <PlayerMovement> (); } // Use this for initialization void Start () { playerCurrentHealth = startingHealth; } // Update is called once per frame void Update () { if(playerCurrentHealth < 0) { // gameObject.SetActive(false); // Loads current scene if player dies SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex); } // Tells the game which way the characters should be thrown when hit by an enemy. if (knockedBack) { if (knockTowardsRight) { transform.position -= new Vector3 (knockbackForce * Time.deltaTime, 0, 0); } else { transform.position += new Vector3 (knockbackForce * Time.deltaTime, 0, 0); } if (knockedUp) { transform.position += new Vector3 (0, knockupForce * Time.deltaTime, 0); } else if (!GetComponent <PlayerMovement> ().controller.m_Grounded) { transform.position -= new Vector3 (0, knockupForce * Time.deltaTime, 0); } } } // Makes it possible to decide how much health the player should have public void HurtPlayer(int damageToGive) { // Set the damaged flag so the screen will flash. damaged = true; // Reduce the current health by the damage amount. playerCurrentHealth -= damageToGive; // Set the health bar's value to the current health. healthSlider.value = playerCurrentHealth; // Play the hurt sound effect. playerAudio.Play (); // If the player has lost all it's health and the death flag hasn't been set yet... if (playerCurrentHealth <= 0 && !isDead); // ... it should die. Death (); } void Death () { // Set the death flag so this function won't be called again. isDead = true; // Tell the animator that the player is dead. anim.SetTrigger ("Die"); // Set the audiosource to play the death clip and play it (this will stop the hurt sound from playing). playerAudio.clip = deathClip; playerAudio.Play (); // Turn off the movement and shooting scripts. PlayerMovement.enabled = false; } // void OnCollisionEnter2D (Collision2D col) { if (col.gameObject.tag == "AI") { knockedBack = true; knockedUp = true; if (col.transform.position.x > transform.position.x) { knockTowardsRight = true; } else { knockTowardsRight = false; } Invoke ("StopKnockback", knockbackTime); Invoke ("StopKnockup", knockbackTime / 2); } } void StopKnockback () { knockedBack = false; } void StopKnockup () { knockedUp = false; } } // // If the player has just been damaged... // if(damaged) // { // // ... set the colour of the damageImage to the flash colour. // damageImage.color = flashColour; // } // // Otherwise... // else // { // // ... transition the colour back to clear. // damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashspeed * Time.deltaTime); // } // // Reset the damaged flag. // damaged = false;
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.