[2D][C#]NullReferenceException: Object reference not set to an instance of an object

Nothing much to say,kinda new to unity, google doesnt explain how to fix nullreferenceexceptions,soo here I am forum.Could someone explain fixing NullReferenceExceptions?I kinda know how to find the part where the error occurs(I think)but I dont have any clue how I should fix it.

NullReferenceException: Object reference not set to an instance of an object
Groundcheckingfix.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/scripts/Groundcheckingfix.cs:17)

NullReferenceException: Object reference not set to an instance of an object
Groundcheckingfix.OnTriggerStay2D (UnityEngine.Collider2D col) (at Assets/scripts/Groundcheckingfix.cs:24)

NullReferenceException: Object reference not set to an instance of an object
Groundcheckingfix.OnTriggerExit2D (UnityEngine.Collider2D col) (at Assets/scripts/Groundcheckingfix.cs:30)

Groundchecking script

using UnityEngine;
using System.Collections;

public class Groundcheckingfix : MonoBehaviour {

    private Player Myplayer;


    void start()
    {
        Myplayer = gameObject.GetComponentsInParent<Player> () [0];

    }

    void OnTriggerEnter2D(Collider2D col)
    {   
        Myplayer.Grounded = true;

    }


    void OnTriggerStay2D(Collider2D col)
    {
        Myplayer.Grounded = true;

    }

    void OnTriggerExit2D(Collider2D col)
    {
        Myplayer.Grounded = false;

    }
}

NullReferenceException means that whatever you’re trying to access is equal to null, which means it doesn’t exist.

The problem with this is that you’re using “start” on line 9.

In C#, case matters. It should be “Start” instead

1 Like