Hello everyone. I have made a circle on screen. I want to see when to similar objects come inside the circle. I get this error:
NullReferenceException: Object reference not set to an instance of an object
GameControlerChecker.OnClickEvent () (at Assets/GameControlerChecker.cs:25)
This is the GameControllerChecker Main script:
public void OnClickEvent()
{
int temp1 = CheckerRight.instance.activeball;
int temp2 = CheckerLefty.instance2.activeball;
if (temp1 == temp2)
{
Debug.Log("worked");
score++;
scoreTxtLbl.text = score.ToString();
}
else
{
if (score > 0)
{
score --;
scoreTxtLbl.text = score.ToString();
} else {
score = 0;
scoreTxtLbl.text = score.ToString();
}
}
}
Ready to provide anything else required, please please please help
Where exactly is line 25 in your GameControlerChecker.cs?
Have you tried attaching unity to the visual studio debugger? Please watch this. Put breakpoint in your code and step through and inspect the values as the game goes through the code.
I could be wrong, but (at Assets/GameControlerChecker.cs:25) means that there is an error on line 25, and debuggers usually overshoot one line when compiling, meaning your error is on line 24. From the looks of your script there is an extra bracket on line 24! Just erase the bracket and it may solve your problem. Let me know if it does!
If there is an extra bracket he would have gotten a compile error instead. Just attach the debugger. What he posted is just he method and not the full file, so line 25 is not the line 25 he posted.
You’ll get a null referenceexception whenever you are trying to access a method or property of an object that hasn’t been instatiated. The lines where you access objects are
int temp1 = CheckerRight.instance.activeball;
int temp2 = CheckerLefty.instance2.activeball;
scoreTxtLbl.text = score.ToString();
scoreTxtLbl.text = score.ToString();
So, you must check CheckerRight and CheckerLeft , and the score variable… are you intializing those variables correctly? are the instances inside the checker initialized correctly?