NullReferenceException

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

So none help to get? I still haven’t figured out this error

Apparantly, playet is null, which makes sense because you never assign it

Then how can i assign the player? This script worked perfectly fine in the JS version

You need to initialize the player variable. You can do it in the Start method:
player = this.gameObject.transform;,
or you can make it public and then, in the editor, drag and drop your player object inside the player variable field (inside the inspector.)

Alternativally, if you want it to stay private you can give it the [SerializeField] attribute:

[SerializeField]
private Transform player;

You will still be able to assign it in the inspector despite the fact that the field isnt public

I am new to unity so i don’t know what to switch “this” with.

Thank you, i completely forgot that command exists