I keep getting the error “NullReferenceException : Object reference not set to an instance of an object Health.Update () ( at Assets/Scripts/Health.cs:21)”
Here is my code:
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
float lastPosistionY = 0f;
float fallDistance = 0f;
float currentHealth = 20f;
Transform player;
private CharacterController controller;
// Use this for initialization
void Start () {
controller = GameObject.Find("FirstPersonCharacter").GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
if(lastPosistionY > player.transform.position.y)
{
fallDistance += lastPosistionY - player.transform.position.y;
}
lastPosistionY = player.transform.position.y;
if(fallDistance >= 5 && controller.isGrounded)
{
currentHealth -= 5;
ApplyNormal();
}
if(fallDistance <= 5 && controller.isGrounded)
{
ApplyNormal();
}
}
void ApplyNormal() {
fallDistance = 0;
lastPosistionY = 0;
}
}