Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Divinum_Laminas · Nov 26, 2015 at 04:14 AM · c#error messagescreen

NullReferenceException

Hi everyone.

I'm trying to write a simple 12 hour timer in C# to be displayed on the canvas. The time itself is displaying, but does not count up because Unity keeps spitting back this error:

NullReferenceException: Object reference not set to an instance of an object timeOfDay.Update () (at Assets/scripts/timeOfDay.cs:28)

The weird thing is, the object on line 28 is declared

 timer.text = time;

and when I build the code inside of MonoDevelop, it returns no errors whatsoever. If i comment out this part, Unity returns the same error but on line 37

 amPM.text = "pm";

and 41

 amPM.text = "am";

As you can see in my full code below, these 3 things are declared, and they are assigned via the inspector.

 using UnityEngine;
 using System.Collections;
 
 using UnityEngine.UI;
 using System;
 
 public class timeOfDay : MonoBehaviour {
 
     float myTimer;
     public Text timer;
     public float modifier;
     TimeSpan t;
     public bool TOD;
     public Text amPM;
 
 
     void Update () 
     {
 
         t = TimeSpan.FromSeconds (Time.time * modifier) + new TimeSpan (4, 0, 0);
         
         if (t.Hours >= 12) {
             t -= new TimeSpan (12, 0, 0);
         }
 
         string time = string.Format("{0:00}:{1:00}", t.Hours, t.Minutes);
         Debug.Log (time);
         timer.text = time;
         
         if (t.Hours >=11 && t.Minutes >= 59) 
         {
             TOD =! TOD;
         }
         
         if (TOD == false) 
         {
             amPM.text = "pm";
         }
         else 
         {
             amPM.text = "am";
         }
     }
 }
 

Thanks in advance for your help, really not sure what I'm doing wrong.

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
1
Best Answer

Answer by iwaldrop · Nov 26, 2015 at 04:22 AM

Honestly it seems like the Text components are not assigned. Those are the only things there that could be null.

Text.text is a property, it's simply a string value, and your time field is assigned just above; even if it was null you can assign null to a string, so there won't be any problem there either.

The only thing that is referenced on line 28 is your Text Component, so I'd double check that it is indeed assigned, or that you don't have two of them in your scene accidentally where one is not.

Also, you should maybe put in some checks in OnEnable that all the things that will be needed in Update are present. You can set enabled to false and log an error or warning if they're not.

Comment
Add comment · Show 4 · 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 Divinum_Laminas · Nov 26, 2015 at 04:35 AM 0
Share

Thanks for the fast reply!

That fixed the error message, so big thank you for that. Only issue now is the time still does not move, it stays stuck at 4:00pm. Any ideas why that might be?

avatar image iwaldrop Divinum_Laminas · Nov 26, 2015 at 04:40 AM 0
Share

Is the component enabled? Is multiplier set to a value greater than 0?

avatar image Divinum_Laminas iwaldrop · Nov 26, 2015 at 04:42 AM 0
Share

Yeah it's enabled and multiplier is set to 2.

Show more comments

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

35 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

64,24): warning CS0219: The variable `nextTetromino' is assigned but its value is never used, (131,163): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected 1 Answer

Unity ads initialize error 1 Answer

object reference is required to access non-static member - What does this mean? 1 Answer

Compiler telling me raycast.collider is obsolete 1 Answer

Game Over when character enters in lighted area 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