- Home /
script GetComponent, nullReference error
So I'm messing around with a countdownTimer script for my Unity project. The Timer is attached to a GUIText GameObject in the scene and its functions are called in a seperate script, attached to a seperate GameObject.
I am using GetComponent to access the timer script. But I am ending up with a NullReferenceException: Object reference not set to an instance of an object error
countdownTimer.cs
 using UnityEngine;
 using System.Collections;
 
 public class countdownTimer : MonoBehaviour
 {
     //countdownTimer: methods to handle a countdown timer
     //it is always assumed that there is a guiText item available for the display output
      
     //PRIVATE MEMBERS
     private bool b_timer_active; //switch to start/stop timer
     private float fl_start_time; //start time (in seconds)
     private float fl_time_left; //time left (in seconds)
      
     //PUBLIC METHODS
     float getFlRemainingTime() { //get the time remaining on the clock
         return fl_time_left;
     }
      
     public void setTimerState(bool b_active_fp) { //set the active state of the timer
         b_timer_active = b_active_fp;
     }
      
     public void setStartTime(float fl_time_fp) { //set the starting value for the countdown
         fl_start_time = fl_time_fp;
     }
      
     void Update() {
         if (b_timer_active) { //check to see if the timer is "on"
             if (!guiText) { //check for an available GUIText component
                 Debug.Log("countdownTimer needs a GUIText component!");
                 enabled = false;
                 return;
             } else {
                 doCountdown(); //decrement the time and send value to GUIText for output
             }
         }
     }
      
     //PRIVATE METHODS
     private void doCountdown() { //
         if (fl_start_time > 0) { //make sure that we had a starting time value before conting down
             fl_time_left = fl_start_time - Time.time;
             fl_time_left = Mathf.Max(0, fl_time_left); //don't let the time fall below 0.0
             guiText.text = outReadableTime(fl_time_left); //display the time to the GUI
             if (fl_time_left == 0.0) { //if time has run out, deactivate the timer and call the followup method
                 b_timer_active = false;
                 guiText.text = "done!";
             }
         } else {
             Debug.Log("countdownTimer needs a value set for fl_time_left");
         }
     }
      
     private string outReadableTime(float fl_time_fp) { //format the floating point seconds to M:S
         int i_minutes;
         int i_seconds;
         int i_time;
         string s_timetext;
         i_time = Mathf.CeilToInt(fl_time_fp);
         i_minutes = i_time / 60;
         i_seconds = i_time % 60;
         s_timetext = i_minutes.ToString() + ":";
         s_timetext = s_timetext + i_seconds.ToString();
         return s_timetext;
     }
 }
This is the script I tried using to test it.
MouseTest.cs
 using UnityEngine;
 using System.Collections;
 
 public class MouseTest : MonoBehaviour {
 
     countdownTimer o_countdownTimer;
 
     // Use this for initialization
     void Start () {
 
         o_countdownTimer = GetComponent<countdownTimer>();
         o_countdownTimer.setStartTime(90.0f);
         o_countdownTimer.setTimerState(true);
     }
 }
What this should do is, as soon as the game starts, the GUIText should start displaying numbers representing the countdown. However, I get an error "NullReferenceException: Object reference not set to an instance of an object" instead. What am I missing?
Answer by LukeAntConroy · Feb 21, 2013 at 01:27 PM
I think you need to find the game object before you can call the methods since they are on different game objects. So I call gui Text gameobject "TESTCON"
using UnityEngine; using System.Collections;
public class MouseTest : MonoBehaviour {
 public GameObject GO_test;
 countdownTimer o_countdownTimer;
 void Start () 
 {
     GO_test = GameObject.Find("TESTCON");
     GO_test.GetComponent< countdownTimer >().setStartTime(90.0f);
     GO_test.GetComponent< countdownTimer >().setTimerState(true);
     
 }
} 
Ah, referencing the object first like how you did it works. It's strange, in a separate project I was able to use getComponent() on just the script without needing to reference the parent object. Oh well. 
Your answer
 
 
             Follow this Question
Related Questions
Using Scripts in AssetBundles 0 Answers
Trouble accessing scripts (Boo) 1 Answer
Deactivate Script 1 Answer
C# GetComponent / change values throught other script 3 Answers
GetComponent not working 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                