Null Reference
These scripts were working earlier but now it says that there is a null refference.
NullReferenceException: Object reference not set to an instance of an object PlayerHealth.AddHealth (Int32 amount) (at Assets/Scripts/Player/PlayerHealth.cs:56) PlayerAddHealth.OnTriggerEnter (UnityEngine.Collider player) (at Assets/Scripts/Player/PlayerAddHealth.cs:24)
using UnityEngine;
using System.Collections;
public class PlayerAddHealth : MonoBehaviour
{
public int addHealth = 10;
public int startingHealth = 100;
public int currentHealth;
public GameObject player;
public PlayerHealth playerHealth;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player");
playerHealth = player.GetComponent<PlayerHealth> ();
}
void OnTriggerEnter (Collider player)
{
if (playerHealth.currentHealth > 0)
{
playerHealth.AddHealth (addHealth);
}
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
I dont really see the problem in any of these but I'm wrong so help would be appreciated.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public int maxHealth = 100;
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;
PlayerShooting playerShooting;
bool isDead;
bool damaged;
void Awake ()
{
anim = GetComponent <Animator> ();
playerAudio = GetComponent <AudioSource> ();
playerMovement = GetComponent <PlayerMovement> ();
playerShooting = GetComponentInChildren <PlayerShooting> ();
currentHealth = startingHealth;
}
void Start ()
{
currentHealth = maxHealth;
}
void Update ()
{
if(damaged)
{
damageImage.color = flashColour;
}
else
{
damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
damaged = false;
}
public void AddHealth (int amount)
{
currentHealth += amount;
HealthSlider.value = currentHealth;
if (currentHealth <= 0)
{
currentHealth = 0;
}
if (currentHealth >= maxHealth)
{
currentHealth = maxHealth;
}
}
public void TakeDamage (int amount)
{
damaged = true;
currentHealth -= amount;
HealthSlider.value = currentHealth;
playerAudio.Play ();
if (currentHealth <= 0 && !isDead)
{
Death ();
}
}
public void Death ()
{
isDead = true;
playerShooting.DisableEffects ();
anim.SetTrigger ("Die");
playerAudio.clip = deathClip;
playerAudio.Play ();
playerMovement.enabled = false;
playerShooting.enabled = false;
}
public void RestartLevel ()
{
Application.LoadLevel (Application.loadedLevel);
}
}
Answer by FlyingHighUp · Dec 08, 2015 at 09:15 PM
Basically "HealthSlider is null. In PlayerHealth, line 56"
Is "HealthSlider " null/empty in the inspector for PlayerHealth?
Yeah your right thanks! I miss the smallest things sometimes.
Your answer
Follow this Question
Related Questions
Faulty NullReferenceException? 2 Answers
Bogus Null Reference in Build only,Bogus Phony NULL REFERENCE in BUILD ONLY (works in editor) 0 Answers
NullReference Exeption thrown even when checked against 2 Answers
Im getting NullReferenceException error 1 Answer
Using TextMeshProUGUI variable in a static function is returning a NullReferenceException 0 Answers