- Home /
Null Reference Exception confusion
Hello everybody!
I am presently having a lot of trouble with a script I have recently made. Here is the script below:
#pragma strict
private var goals : GameObject;
function Update ()
{
if(goals.GetComponent(GoalScript1).goal == true)
{
if(goals.GetComponent(GoalScript2).goal == true)
{
if(goals.GetComponent(GoalScript3).goal == true)
{
Debug.Log("Win!");
Time.timeScale = 0;
}
}
}
Basically, the script looks at a variable found in each of the scripts mentioned in the three nested if statements and if all of the three if statements come back true, then (for now) it Logs a Win and pauses the game. Later I will attach GUI stuff to it.
The problem is the line of the first if statement is coming up as a Null Reference Exception which, according to my research, means that it is looking for a Object or Component that does not exist. Of course, the item DOES exist.
I have three goal scripts each having the boolean variable goal and each are coming up true according to my Debug.Log in the individual scripts.
Please tell me what I'm doing wrong. I've checked my syntax a hundred times, I got the basic concept from the script here: http://www.unityscript.com/lessons1/lessons/Communicating_outside_of_your_script.html
I would appreciate any help you can give me!
Thanks a lot!
Dwayne
This is not an answer, it's just my final working script. It may change, however, if Gregzo or someone else can suggest a better function to put my if statements in. Thanks for everyone's help!! Y'all rock!!
#pragma strict
private var goal1 : GameObject;
private var goal2 : GameObject;
private var goal3 : GameObject;
function Awake()
{
goal1 = GameObject.Find("GoalTrigger_1");
goal2 = GameObject.Find("GoalTrigger_2");
goal3 = GameObject.Find("GoalTrigger_3");
}
function Update ()
{
if(goal1.GetComponent(GoalScript1).goal == true)
{
if(goal2.GetComponent(GoalScript2).goal == true)
{
if(goal3.GetComponent(GoalScript3).goal == true)
{
Debug.Log("Win!");
Time.timeScale = 0;
}
}
}
}
You could read the scripts at the start (see Grezo's answer.) Consider it for new stuff you write -- can be easier to work with.
Also, do you really need three different goalScripts? For example, if they only have different point values, reuse the same script, but add public points : int;
and select points for each.
Answer by gregzo · Apr 10, 2012 at 09:02 PM
Hi!
First, I doubt you need to check that every frame. Try taking it out of the update function and checking only when you need to!
Second, do you have 3 sperate scripts, named GoalScript1, GoalScript2 and GoalScript3? Are they attached to your object? Can you see them attached in the inspector?
Last, try this very redundant version :
var goalScript1 : GoalScript1;
goalScript1 = goals.GetComponent(GoalScript1) as GoalScript1;
Debug.Log(goalScript1.goal);
If you're still getting a null reference, then there is no script of type GoalScript1 attached to your goals object.
The thought behind putting it in the function Update was that at any time all of my goals might trigger and I would need to register the win ASAP. What other function should I put it in?
Thanks for your input though!
Answer by Lttldude · Apr 10, 2012 at 08:56 PM
Since you are using #pragma strict, the format should be
goals.GetComponent.<GoalScript1>().goal
Resources: http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html
No. goals.GetComponent(GoalScript1).goal
does the same thing as the generic version, but slightly faster. There's no reason to use the generic version of GetComponent when using Unityscript, #pragma strict or not. If there was any problem with the syntax, there wouldn't be a question about null reference exceptions, since the script wouldn't compile in the first place....
I did not know that! I heard that one ought to use #pragma strict if they're developing for mobile or iOS, or something like that, so I kept it.
@ChickenReborn: keeping #pragma strict is fine. Using GetComponent(Type)
ins$$anonymous$$d of GetComponent.<Type>()
is also fine, in fact it's preferable in Unityscript.
Answer by rutter · Apr 10, 2012 at 08:59 PM
This is the line that's erroring out, yes?
if(goals.GetComponent(GoalScript1).goal == true)
If so, we have two obvious possibilities:
goals
is nullgoals.GetComponent(GoalScript1)
is null (implying there is no such component attached)
Looking at the script sample you've posted, it looks like you never set goals
. Perhaps you could make that variable public and set it in the inspector? Or perform some quick lookup in Awake()
using GameObject.Find()
, more or less like this:
function Awake()
{
goals = GameObject.Find("MyGoalsObject"); //fill in the right name
}
Indeed, if goals is private, you need to assign the object to it somehow...
What if I need to find three objects as I need to access three scripts, each of which is attached to its own Game Object?
Also, concerning "setting" the variable named goals, does that mean tell it what object to look for that the script is attached to, because I tried that. I created three separate variables (goal1, goal2 and goal3) and connected each to the game objects connected to the scripts I am trying to connect to and I still got errors.
Finally, I don't know why its giving me the Null Exception error on the one if statement and not on the other two, which are set up identically.
Thanks for the input!
Belay those questions! I created three separate variables, set them all in the Awake function and it WOR$$anonymous$$ED!! AWESO$$anonymous$$E THAN$$anonymous$$S!!