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 /
avatar image
0
Question by Eco-Editor · Jun 20, 2017 at 01:00 PM · uitimernull reference exception

Why do I get a null reference with this timer?

Hello all,

I can't put my eyes on why do I get a null reference for this C# code for a timer:

     public Text timerText;
     private float startTime = 300f;
 
     private void Start()
     {
         timerText = GetComponent<Text>();
     }
 
     private void Update()
     {
         startTime -= Time.deltaTime;
         string minutes = (startTime / 60).ToString();
         string seconds = (startTime % 60).ToString("f0");
 
         timerText.text = string.Format("{0:0} : {0:00}", minutes, seconds); //this is the null reference line
     }

and here's how it looks in the editor.alt text Also for some reason I get double numbers in the text window

Any suggestions??

timecount.jpg (34.9 kB)
Comment
Add comment · Show 4
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 TreyH · Jun 20, 2017 at 01:12 PM 0
Share

is the script on the same object that has the Text component?

avatar image Eco-Editor TreyH · Jun 20, 2017 at 02:56 PM 0
Share

It is, the script is on the text component of the canvas

avatar image hexagonius · Jun 20, 2017 at 01:27 PM 0
Share

Select the error message in the console. It should highlight the gameobject that threw the error. It's proably one you didn't expect.

avatar image Woltus · Jun 20, 2017 at 01:58 PM 0
Share

Where do You attach this script?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Dave-Carlile · Jun 20, 2017 at 01:28 PM

Your timerText variable isn't being set by the GetComponent call in Start.

Since timerText is a public field it should show up in the inspector. Is there a reason you're not just dragging the Text reference to that field and setting it at edit time? That is the generally recommended approach for hooking up references.

Comment
Add comment · Show 5 · 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 Eco-Editor · Jun 20, 2017 at 01:32 PM 0
Share

Yes thank you! you're right!

avatar image NoseKills Eco-Editor · Jun 20, 2017 at 02:33 PM 0
Share

This is not a good answer to this problem.

avatar image Eco-Editor NoseKills · Jun 20, 2017 at 02:59 PM 0
Share

But it solved my problem... is that a coincidence?

Show more comments
avatar image Dave-Carlile Eco-Editor · Jun 20, 2017 at 02:41 PM 0
Share

I converted your answer to a comment for you. :)

avatar image
0

Answer by hexagonius · Jun 20, 2017 at 07:15 PM

It's not at all recommended. If the script is on the same gameobject adding the RequireComponent attribute is recommended.

Comment
Add comment · Show 3 · 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 Eco-Editor · Jun 22, 2017 at 10:08 AM 0
Share

Hi @hexagonius I've added the RequireComponent and it's working well. Thanks!

avatar image Eco-Editor · Jun 22, 2017 at 10:19 AM 0
Share

any idea why I get double numbers in the text window?? (see picture above)

avatar image TreyH · Jun 22, 2017 at 11:23 AM 0
Share

you had two '0' keys in your format string template. change to:

 timerText.text = string.Format("{0: 0} : {1: 00}", $$anonymous$$utes, seconds); //this is the null reference line

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

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

Related Questions

Null reference exception? 1 Answer

Want my UI to not move when I move my camera? 2 Answers

How can i link my code together so the score value affects the timer value 1 Answer

Updating stamina in UISlider 1 Answer

Unity 5 - Time counter Up script (millisecond precision) UI 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