How do I fix: Object reference not set to an instance of an object


This issue was answered successfully. The fix was to change:

levelManagerRef = GetComponent<LevelManagerScript>();


levelManagerRef = GameObject.Find("LevelManager").GetComponent<LevelManagerScript>();

I removed the Boo code from this initial post to prevent confusion. (You’ll find the C# script in the comment below.)

I hope this helps other people in the future; it was a really simple oversight and fix. If this does help you, please be sure to give Linus a thumb’s up for his answer.


I tried to search for the answer, but the results I found didn’t help me. I am learning Unity as well as C#/Boo code at the same time, so I’m new … and quite ignorant it seems, because this is killing me.

I am creating a simple brick breaker game to learn some fundamentals of Unity and coding. For the most part things are going well; however, my LevelManager isn’t working properly.

What I want it to do is:
~ Count the number of Bricks with the Bricks tag.
~ Decrement the counter as bricks are destroyed.
~ When all of the bricks are destroyed, change level. (Right now I just have it printing “You win.” I’m good with the Application.LoadLevel function.)

Everything is working except the brick counter. When a brick is destroyed I get the following error: “Object reference not set to an instance of an object” - (Line 19 of the BrickScript_Normal script: “levelManagerRef.BrickCounter()” ). I don’t see any other way to make this call, so … yeah. This is where I need the help.

While I am using Boo, I’m good with answers/suggestions in C# or Boo.
Thank you in advance for your assistance!!

I’ve already had a suggestion to have the KillBrick() method (of the BrickScript_Normal script) perform this function as opposed to a LevelManager script, but I have bricks with varying hit points, so I’m looking for a central manager to perform this operation when I’m using BrickScript_Normal, BrickScript_Strong, etc.

You where looking for level manager component on the brick itself.
Have the levelManager on a game object called gaemmanager, or enter another name in GameObject.Find

I changed the LevelManagerScript to be private not static, it now shows in the inspector. For some reason BrickHitpoints would also not show in the inspector while that was static. I can only conclude that static is from the devil.

using UnityEngine;

using System.Collections;

public class BrickScript_Normal : MonoBehaviour
    private LevelManagerScript levelManagerRef;

    int brickHitPoints = 1;

    void Start()
        levelManagerRef = GameObject.Find("gamemanager").GetComponent<LevelManagerScript>();

    void Update()

    void OnCollisionEnter(Collision brickCollider)
        if (brickHitPoints <= 0)

    void KillBrick()
        //I also switched around these two lines


Try to invert calling order in KillBrick() levelManagerRef.BrickCounter()

put it like: levelManagerRef.BrickCounter() KillBrick()