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);
}
}