0

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);
score += 10;
guiScore.text= "Score: " + score;
renderer.material.mainTexture = blue;
isBlue = true;

}
}
user3349271
  • 5
  • 1
  • 4
  • Search for the error message. Read some of the questions and answers. Use this knowledge to come up with some hypothesis. Test them. – user2864740 Mar 05 '14 at 04:15
  • Searching for the error suggests `.net` but I can't confirm. – Kon Mar 05 '14 at 04:18
  • @Kon NullReferenceException is indeed [more likely] from .NET/CLR, as [NullPointerException](http://stackoverflow.com/questions/4648476/why-does-java-have-nullpointerexception-instead-of-nullreferenceexception) is from the JVM. I have no idea *what* is going on with this question :) – user2864740 Mar 05 '14 at 04:19
  • Sorry This is a Java Script for a Game Engine called Unity 3D. The programming is essentially the same as just Java. – user3349271 Mar 05 '14 at 04:24
  • @user3349271 Well, no. It's an entirely different language and run-time. However, the *cause* and *solution* for such an error is the same as a Java-NPE a C#-NRE. See my first comment, which suggests *searching* for the error message - then you'll have some ideas about what sort of issue causes such an exception .. – user2864740 Mar 05 '14 at 04:26
  • It's Unity's UnityScript, and it looks like JavaScript, but it's not. Hence it *doesn't* look like Java. – Roberto Mar 05 '14 at 05:25
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – CodeSmile Feb 16 '15 at 10:47

2 Answers2

0

It's really easy to find the null reference, just check the error message and see which line the error occurred.

But looking at your code, it can be in this line:

renderer.material.mainTexture = blue;

The problem there would be that you have a renderer without a material, or no renderer component in this Monobehaviour.

It could also be here:

guiScore.text= "Score: " + score;

This would be a problem if there's no guiScore set in this Monobehaviour.

Roberto
  • 11,557
  • 16
  • 54
  • 68
  • I know where the null reference is. it is the "renderer.material.mainTexture = blue;" line. I got rid of the guiScore line and have the same problem. I just forgot to take it out when I posted this question. the line "renderer.material.mainTexture = blue;" Words in another line of code in another class to change the texture of a ball. I have it loaded into the object so I know thats not the problem either. – user3349271 Mar 05 '14 at 05:43
  • Either `renderer` is null or `material` is null, there's no other way to get this error. – Roberto Mar 05 '14 at 10:56
0

renderer.material.mainTexture can also be Null because the shader that the material is pointing to does not have a "_MainTexture" parameter. You can verify this by looking at the material and seeing whether there is a "Base (RGB)" field:

Making sure the material has a _MainTexture property

pek
  • 17,847
  • 28
  • 86
  • 99