• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MoloMowChow · Feb 21, 2013 at 12:36 PM · gameobjectnullreferenceexceptiongetcomponent

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     
 }

} alt text


capture.png (151.5 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MoloMowChow · Feb 21, 2013 at 11:15 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Deactivate Script 1 Answer

Trouble accessing scripts (Boo) 1 Answer

C# GetComponent / change values throught other script 3 Answers

Using Scripts in AssetBundles 0 Answers

Why is this null? Finding a script on an object 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges