- Home /
Object reference not set to an instance of an object issue
For some odd reason, my If statement in the Paddle class keeps giving me an error. The error says "NullReferenceException: Object reference not set to an instance of an object". I cant wrap my head around this problem. I need to get the Boolean value of "isBlue" from the Ball Class into the Paddle class. Once it is in the Paddle class, I need to use that boolean value to transform a texture. Any Help would be greatly Appreciated. Thanks
//
Paddle class
#pragma strict
var blue: Texture;
var isBlue: boolean = false;
Public var newBall : Ball;
function Start () {
}
function Update () {
newBall = GetComponent(Ball);
isBlue = newBall.isBlue;
if(isBlue == true){
renderer.material.mainTexture = blue;
}
}
Ball Class
var blue : Texture;
var isBlue : boolean = false;
function OnCollisionEnter(col : Collision){
if(col.collider.name == "Brick3"){
Destroy(col.gameObject);
renderer.material.mainTexture = blue;
isBlue = true;
}
}
Answer by robertbu · Mar 05, 2014 at 05:55 AM
This line will fail if the GetComponent(Ball) call fails to find the 'Ball' component. Note this call is expecting that the 'Ball' script will be on the same game object as the script above. If it is on another game object (like I would expect since this is the Paddle script), then you need to first get access to that other game object and then use that game object in the GetComponent() call:
newBall = GameObject.Find("Ball").GetComponent(Ball);
Where the ball object has the name 'Ball'. Note it would be more efficient to put the GetComponent() call in Start() and only call it once if there is only one ball during the life of the game.
Thank you very $$anonymous$$uch Sir. You have no idea how much I appreciate this answer. It solved my problem I have been trying to figure out for almost 24 hours. I was starting to give up. Thank you so much/
it would be more efficient to put the GetComponent() call in Start() and only call it once
Second that! Don't GetComponent or Find in Update; it is never necessary to do that and will slow your game down.
Also, much less important tip: you never need to say if (someBool == true)
, because it is equivalent to say " if (someBool)
.
Less obviously, you can check if (newBall)
. If there is no newBall, it will return false, and you can avoid doing whatever was going to cause the error until there is one.
I'm a little confused as to what @$$anonymous$$iloblargh is saying about the boolean. how is if(someBool==true) equal to if(someBool). if someBool is a variable it can be either true or false. and depending on its value it could change the output of the program. $$anonymous$$aybe Im missunderstanding what you talking about. Could you futher explaine. And I have another question for both of you. @robertbu I put the "newBall = GameObject.Find("Ball").GetComponent(Ball);" statment in the start method and just put my if statements in the update method. Will that also slowdown my game?
I got rid of if(isBlue == true) and replaced it with if(isBlue) and the program ran just fine. Im still confused as to how they are the exact same statement though. cause what if isBlue is set to false. lol
GameObject.Find() is slow. Doing it once in Start(), or once in a while as the game is running is fine. But doing it in Update() is discouraged. Think about how many times things execute in Update. If your app is running at 60 fps, in five $$anonymous$$utes of game play the function would execute 18,000 times. With that said, chances are it would not slow down your game (based only on your class names). If your game is running at 60 fps you can do a certain amount of work during a single frame. So your game needs a certain amount of work/complexity before it cannot get all its work done in 1/60 of a second and therefore slows down. A typical pong-like game does not do much work in a frame, so adding a GameObject.Find() every frame would not slow it down. Note if you needed to get it every frame, finding your object by tag is efficient enough to use every frame. So you would tag your ball 'Ball' and then you could use GameObject.FindWithTag().
As for the 'if' statement, the 'if' will execute if whatever is inside it is true. So a way to think about it is to replace 'isBlue' with its value. So if 'isBlue' is 'true', then it is equivalent to 'if (true)' which will execute. Once you get your brain wrapped around that one, then figure out the opposite. I see a lot of beginning programmers write something like 'if (isBlue == false)' that is equivalent to the the more professional 'if (!isBlue)'.
Your answer
Follow this Question
Related Questions
Getting java.lang.Exception error on android and game ends up crashing. 0 Answers
NullReferenceException After A Few Hours? 1 Answer
Object reference not set to an instace of an object (JS) 0 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer
NullReferenceException 3 Answers