NullReferenceException: Object reference not set to an instance of an object

hey, I’m quite new to unity and coding so please pardon my ignorance, I’m getting this error on both line 20 and 49, I’ve marked “here” in a comment to show the lines,and I’ve spent a few hours trying to figure out whats wrong but I can’t. I know its probably something very trivial, any help is appreciated.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 public class Character1_Controller : MonoBehaviour {
     public float maxSpeed = 10f;
     bool facingRight = true;
     public int score = 0;
     private Display display;
     Animator anim;
     void Start () {
         anim = GetComponent<Animator>();
         display = GetComponent<Display>();
     void FixedUpdate () {
         float move = Input.GetAxis("Horizontal");
         anim.SetFloat("speed", Mathf.Abs(move));
         GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
         if (move > 0 && !facingRight)
             Flip ();
         else if (move < 0 && facingRight)
             Flip ();
     void Flip () {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     void OnTriggerEnter2D(Collider2D hit){

display = GetComponent();

If the gameobject does not have a DisplayComponent attached this function will return null.
In you next line you USE this value, WITHOUT first checking to see if it’s null.


You should always check such a value, before using it.

   Debug.Log("Problem, could not find Display script attached to gameObject " +;

When searching unity answers be sure to read the answers of “similar” questions, even if the question is not exactly the same, the answer might have the info you need (this Q has been asked and answered MANY times).