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 TheDavyStar · Dec 27, 2015 at 11:03 AM · c#unity 52d

Object reference not set to the instance of an object

Hey all, So, today I've been trying to program a timer for a game I'm making. In the game, the timer counts soun from 60 seconds to 0, and at 0, the game ends. Just as a placeholder for the moment ( because I can't seem to get it to count down in seconds) I have set the timer value to 60, and applied the scrit for the timer to a text so the value is seen visibly counting down. Here is the script I have:

  using UnityEngine;
  using UnityEngine.UI;
  using System.Collections;
  
  public class Timer : MonoBehaviour {
      public int timer;
      public Text timerText;
      public GameObject Timer
  
      // Use this for initialization
      void Start () {
          timer = 600;
  
      }
      
      // Update is called once per frame
      void Update () {
  
          timerText.text = timer.ToString (Timer);
          
          timer -= 1;
      }
  }

The error seems to happen line 18. It is indeed, rather annoying. Any and all help and pointers would be greatly appreciated, thanks :)

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

Answer by SoloDeveloper · Dec 29, 2015 at 05:21 AM

  1. Check in Editor if the public Text timerText; is empty

  2. Also change timerText.text = timer.ToString (Timer); to timerText.text = "" + timer;

  3. Also change timer -= 1; to `timer -= Time.deltaTime;

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 Gnorpelzwerg · Dec 29, 2015 at 09:45 AM 0
Share

And you should round the timer value or cast it to int as long as you don't want fractions of seconds to be displayed in timerText.text;

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

1 Button Alternating Between 2 onClick Functions 1 Answer

NullReferenceException: Object reference not set to an instance of an object Plane.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/Plane.cs:37) 0 Answers

How to run a script every time a scene is loaded. 0 Answers

Unlocking & "Equipping" Colors onto Sprites with c# 2 Answers

Books for learning C# by programming 2D games? 2 Answers


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