Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Zaineph · Jan 14, 2015 at 07:17 PM · timernull reference exception

NullReferenceException

Hi all,

I'm fairly new to this so forgive my inexperience.

I am trying to create a timer in unity. I have two scripts. The first is the GameControllerScript which creates a variable gameTime = Time.time. This will start when the game is launched. In the levels (or tracks) I want to start a different timer called track time which only begins when the player starts the track so I have a different script for each level.

Here are the two scripts

 #pragma strict
 
 public var gameTime : float;
 
 function Start () 
 {
     gameTime = Time.time;
 }
 
 function Update () 
 {
 
 }


and the second one

 #pragma strict
 
 private var gameControllerScript : GameControllerScript;
 private var trackTime : float;
 
 gameControllerScript = GetComponent(GameControllerScript);
 
 function Start () 
 {
     
 }
 
 function Update () 
 {
     trackTime = Time.time - gameControllerScript.gameTime;
 }

This is the error that I am getting.

NullReferenceException: Object reference not set to an instance of an object TrackControllerScript.Update () (at Assets/TrackControllerScript.js:15)

Please could someone explain why I'm being told that it is a Null reference.

Regards

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Noah Dyer · Jan 14, 2015 at 07:24 PM

You can't set gameControllerScript as a "class" variable [not sure what the correct nomenclature for such a variable is in JavaScript]. You have to do it in a function. in this case I'd recommend Start() :)

Comment
Add comment · Show 6 · 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 Jeff-Kesselman · Jan 14, 2015 at 07:25 PM 0
Share

Really? I don't know a lot about Unity script but as it has a file level execution scope my assumption was that all the components are created and attached before that scope is evaluated...

avatar image Noah Dyer · Jan 14, 2015 at 07:26 PM 0
Share

To clarify, I'm suggesting you move gameControllerScript = GetComponent(GameControllerScript); to the Start() function.

avatar image Zaineph · Jan 14, 2015 at 07:44 PM 0
Share

Ok I've changed the code to the following

 #pragma strict
 
 private var trackTime : float;
 private var gameControllerScript : GameControllerScript;
 
 function Start () 
 {
     gameControllerScript = GetComponent(GameControllerScript);
 }
 
 function Update () 
 {
     trackTime = Time.time - gameControllerScript.gameTime;
 }

however I'm still getting the same error. I've tried moving the gameTime = Time.time; to the update function in the first script as it looked like it wasn't actually counting.

I'm wondering if I haven't put the scripts in the right folder or something.

avatar image Noah Dyer · Jan 14, 2015 at 08:14 PM 0
Share

Are both of your scripts attached to the same object? If not, the GetComponent() call will return null.

avatar image Noah Dyer · Jan 14, 2015 at 08:21 PM 0
Share

If they're not on the same object, you'll to need to use GameObject.Find(), or create a relationship between the GameController and your timer using class variables and the editor.

http://docs.unity3d.com/ScriptReference/GameObject.Find.html

It'll look something like:

 var gcObject = GameObject.Find("gameControllerObjectName"); 
 gameControllerScript = gcObject.GetComponent(GameControllerScript);




Show more comments
avatar image
1

Answer by Jeff-Kesselman · Jan 14, 2015 at 07:24 PM

Add a Debug.Log in Update and print the value of gameControllerScript.

My strong suspicion is that it is null.

If so, check to make sure the object that script is on really has a GameControllerScript component as well.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Null reference exception? 1 Answer

Why do I get a null reference with this timer? 2 Answers

GUI elements vanish when publishing 1 Answer

Timer Between Labels 2 Answers

Need some help an object collider that stops a timer then displays it on another scene 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges