NullReferenceException, Script Error

Hi,

I wrote a script that should manage that a bool in the animater turns true or false if a BoxCollider2D from the Player called “groundCheck” got triggered by the ground or not. After I finished it and added the script it doesn’t worked as I wanted to. An error says:“NullReferenceExeption: Object refernce not set to an instance of an object […]” I don’t know how to fox it. The error seems to apear in line 16 and 21. There is another post in this forum which tells how to fix it but I don’t understand it.

using UnityEngine;
using System.Collections;

public class GroundCheck : MonoBehaviour {

	private Player player;

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


	void OnTriggerEnter2D(Collider2D col) 
	{
		player.grounded = true;
	}

	void OnTriggerExit2D(Collider2D col)
	{
		player.grounded = false;
	}

}

player is never getting initialized because the place that you think you’re setting it - start() - never gets called.

change it to Start() and it should fix your problem :wink: