Null Reference Exception when referencing other script

Ok, here’s the problem. I’m attempting to reference the Boolean in another script attached to the same game object that the current script is running on, that will tell the new script if the alarm is raised. This would then cue a countdown timer that would begin counting down on the top of the screen.

The game object they are both attached to is called “Player”.

The code for that script is here:

private var startTime;
private var remainingSeconds : int;
private var roundedRemainingSeconds : int;
private var clockSeconds : int;
private var clockMinutes : int;
private var countDown : System.Boolean = true;
var alarmScript : ProgressBar = GetComponent(ProgressBar);

//Response time is measured in seconds, so adjust accordingly
var responseTime : int;

function Awake()
{
	if(alarmScript.alarmRaised == true)
	{
		startTime = Time.time;
	}
}
 
function OnGUI () {

	if(alarmScript.alarmRaised == true)
	{

		if(countDown)
		{
    		//Time is measured from the moment the script is called.
    		var guiClock = Time.time - startTime;
   			remainingSeconds = responseTime - (guiClock);
 
			//This is where you would make events happen, etc, based on the clock.
   			if (remainingSeconds == 60)
    		{
        		print ("One Minute Left");
   			}
    		if (remainingSeconds == 0) 
    		{
				print ("Time is Over");
    		}
 
    		//displays the clock in 00:00 format
			roundedRemainingSeconds = Mathf.CeilToInt(remainingSeconds);
    		clockSeconds = roundedRemainingSeconds % 60;
    		clockMinutes = roundedRemainingSeconds / 60; 
 
 			if(clockSeconds != 0 || clockMinutes != 0)
 			{
    			timerText = String.Format ("{0:00}:{1:00}", clockMinutes, clockSeconds); 
				GUI.Label (Rect (400, 25, 100, 30), timerText);
			}
		
			if(clockSeconds == 0 && clockMinutes == 0)
			{
				countDown = false;
			}
		}
		
		//Stops the countdown and displays a message once it hits 0
		if(!countDown)
		{
			GUI.Label(Rect(400,25,100,30), "Done!");
		}
	}
	
}

However, I’m getting a null reference exception, even when using:

GameObject.Find("Player").GetComponent("ProgressBar").alarmRaised == true

The names are correct:
(The field for the script, I filled in with the “Player” game object, and yet it says no object referenced…)

However I continually get this:
12940-unityproblem2.jpg

Can someone help me get rid of this error?

The fact that you made alarmScript public (by default) means that you need to link it in the Inspector window. If you’re still getting null reference I believe you need to find the variable under Police Timer and then just drag the game object into that slot. See how it says “none (Progress Bar)”? It should be called Alarm Script, and it’s looking for your instance of type ProgressBar.

I found my issue…

function Awake()
{
    if(alarmScript.alarmRaised == true)
  {
       startTime = Time.time;
    }
}

Apparently this is a big no no…

The time cannot be set in the Awake function using an IF statement, so I solved it by adding this line into OnGUI:

if(!timeSet)
{
	startTime = Time.time;
	timeSet = true;
}

timeSet is a bool flag that tells me if I have set a zero time for the clock yet. With this in the on GUI, as soon as alarmRaised is flagged, the zero mark gets set, and stays set properly. The timer then works as expected, and I deleted Awake() altogether, as it wasn’t needed.

Thanks for the suggestions though guys!!