- Home /
How do I fix: Object reference not set to an instance of an object
UPDATE
This issue was answered successfully. The fix was to change:
levelManagerRef = GetComponent<LevelManagerScript>();
to
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.
END OF UPDATE
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.
for clearly making an effort in witting the question and tried to solve yourself. This is a quality null reference question
If you are new to program$$anonymous$$g, Boo might not be the route to go. If not only for the reason that there is less documentation and people to help with Boo.
you scared me off for one.
I understand what you're saying, and I appreciate the suggestion. =) I actually started with C#, but I kept getting nit-pick-y errors that irked me. (Stupid semi-colons!!)
When I started using Boo it 'felt' better and I actually understand it better when I'm reading it. Yes, it's been a pain to research some things, but I can convert C# to Boo so it's been mostly painless.
As for this issue, it seems to be a problem whether I use C# or Boo - at least from what I can tell. I don't think its a syntax error, I think I'm missing something or that I scripted it incorrectly. Then again, I'm new to this and could be horribly mistaken. =)
Well there are those universal null references, like forgetting to place the script on the game object. Happened to me at least once
Yep, I've messed that up before, so, to be certain, I just checked ...
BrickScript_Normal is on my Brick_Normal prefab (and all in-game instances). Level$$anonymous$$anagerScript is on my Level$$anonymous$$anager game object.
Now, if I missed something, like applying a script component/variable, then I've got a problem as no components are asking for a script.
All right, in order to facilitate this process I have reverted my code to C#. Hopefully this helps debug.
NullReferenceException: Object reference not set to an instance of an object BrickScript_Normal.$$anonymous$$illBrick () (at Assets/Code/Scripts/BrickScript_Normal.cs:30)
How do I make it != Null?
Line 30 of BrickScript_Normal :: level$$anonymous$$anagerRef.BrickCounter (1); is not talking to Line 39 of Level$$anonymous$$anagerScript :: public void BrickCounter(int bCount)
When I hover over Line 30 of BrickScript_Normal in $$anonymous$$onoDevelop, it does recognize the public void BrickCounter(int bCount) of the Level$$anonymous$$anagerScript. So, at least it knows who it's supposed to talk to.
I have continued to look up the error, but I'm just not getting it. It's like when my mother would tell me to look up a word I couldn't spell as a child. Well, if I don't know how to spell it how the heck am I supposed to look it up? None of the example code references what I'm trying to do in a way I grasp.
With that said, I'm not the type of person who needs to be told twice, so if I get good example code that fixes my problem I promise not to bother you all again with this issue. =)
Level$$anonymous$$anagerScript.cs
using UnityEngine;
using System.Collections;
public class Level$$anonymous$$anagerScript : $$anonymous$$onoBehaviour
{
private static Level$$anonymous$$anagerScript level$$anonymous$$anager;
GameObject[] bricksRemaining;
int brickCount;
int currentLevel = 1;
void Awake()
{
if (level$$anonymous$$anager == null)
{
level$$anonymous$$anager = this;
if (Application.loadedLevelName == "HomeScreen")
{
DestroyImmediate (this.gameObject);
}
else
{
DontDestroyOnLoad (this.gameObject);
}
}
}
void Start ()
{
bricksRemaining = GameObject.FindGameObjectsWithTag("Bricks");
brickCount = bricksRemaining.Length;
Debug.Log ("Bricks :" + brickCount);
}
void Update ()
{
}
public void BrickCounter(int bCount)
{
brickCount = brickCount - bCount;
Debug.Log ("Bricks Remaining: " + brickCount);
if (brickCount <= 0)
{
Debug.Log ("You win!");
currentLevel++;
Application.LoadLevel("Level" + currentLevel);
}
}
}
BrickScript_Normal.cs
using UnityEngine; using System.Collections;
public class BrickScript_Normal : $$anonymous$$onoBehaviour { Level$$anonymous$$anagerScript level$$anonymous$$anagerRef; int brickHitPoints = 1;
void Start ()
{
level$$anonymous$$anagerRef = GetComponent<Level$$anonymous$$anagerScript>();
}
void Update ()
{
}
void OnCollisionEnter (Collision brickCollider)
{
brickHitPoints --;
if (brickHitPoints <= 0)
{
$$anonymous$$illBrick();
}
}
void $$anonymous$$illBrick()
{
Destroy (gameObject);
level$$anonymous$$anagerRef.BrickCounter (1);
}
}
Answer by Linus · Feb 23, 2014 at 01:40 PM
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)
{
brickHitPoints--;
if (brickHitPoints <= 0)
{
KillBrick();
}
}
void KillBrick()
{
//I also switched around these two lines
levelManagerRef.BrickCounter(1);
Destroy(gameObject);
}
}
Line 11 of BrickScript_Normal (per your example) I added GameObject.Find("Level$$anonymous$$anager") and that solved everything!!
As a side note, my fault for not mentioning that I do have an empty game object labeled "Level$$anonymous$$anager" where the Level$$anonymous$$anagerScript resides.
Thank you very much for the fix!! I really appreciate it.
Thats great. Remember to mark as answered by checking the select answer box, think its on the left side.
Also, I converted your C# version into a comment ins$$anonymous$$d of an answer.
I think best practice for that is to edit into your question, and then place a comment that you have updated your question.
Answer by JJVelazquez · Feb 22, 2014 at 04:32 PM
Try to invert calling order in KillBrick() levelManagerRef.BrickCounter()
put it like: levelManagerRef.BrickCounter() KillBrick()
Thank you for the suggestion. Unfortunately, it didn't work.
I get the same error, but now the bricks won't break. (This info may or may not be useful to someone smarter than me in deter$$anonymous$$ing where the issue lies.)
This is beacause you get an null reference exception when try to call BrickCounter(), and the execution stops there. Your variable level$$anonymous$$anagerRef has no value, I dont use boo, so I cant see what is wrong in your code. You could try debug the value of level$$anonymous$$anagerRef after the line "level$$anonymous$$anagerRef = GetComponent[of Level$$anonymous$$anagerScript]()" to see whether its correctly initialized. In c# it would be like: "Debug.Log(level$$anonymous$$anagerRef.gameObject.name);", with this line of code, you will receive the game object name where is the Level$$anonymous$$anagerScript attached in your console window if your initialization was sucessfull, or null if it wasnt sucessfull. This way you can discover where the problem is starting at.
Your answer
Follow this Question
Related Questions
Calling a function across all instances of an object 2 Answers
'x' is not a member of 'System.Type'. 1 Answer
Instance variable 0 Answers
Script not being executed 1 Answer