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 Vasov97 · Jan 25, 2017 at 11:59 PM · timercount

This code shows me seconds and miliseconds after game started. What should I do because I want to show only seconds ?

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class TimerScript : MonoBehaviour {

 public Text timerText;
 private float secondsCount;
 private int count;

 // Use this for initialization
 void Start () {

     count = 0;
 }
 
 // Update is called once per frame
 void Update () {
     count = count + 1;
     secondsCount += Time.deltaTime;
     timerText.text =" Score:" + secondsCount.ToString();
 }
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 Pengocat · Jan 26, 2017 at 12:58 AM

You could simply use Time.time like this. The zero in "Score:0" is what removes the milliseconds/decimals.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class TimerScript : MonoBehaviour
 {
     public Text timerText;
 
     void Update()
     {
         timerText.text = Time.time.ToString ("Score:0");
     }
 }
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 Vasov97 · Jan 26, 2017 at 01:42 PM 0
Share

That works but with that code time doesn't restart when player dies , and with my code above did.

avatar image Vasov97 · Jan 26, 2017 at 01:44 PM 0
Share

How can I call Text from TimerScript to script where is function to restartlevel ? I know for gameobjects but I don't now how to call this text to restart to 0 when player dies

avatar image Pengocat Vasov97 · Jan 26, 2017 at 02:56 PM 1
Share

In the timer script you could have a public method to reset the timer.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class TimerScript : $$anonymous$$onoBehaviour
 {
     public Text timerText;
     float timer;
 
     void Update()
     {
         timer += Time.deltaTime;
         timerText.text = timer.ToString("Score:0");
     }
 
     public void ResetTimer()
     {
         timer = 0;
     }
 }

Then in anotherScript where you have the method that happens when the player is dead you call the method in TimerScript. In order to get the reference to the instance of TimerScript you drag TimerScript onto the public field timerScript in AnotherScript in the inspector.

 using UnityEngine;
 
 public class AnotherScript : $$anonymous$$onoBehaviour
 {
     public TimerScript timerScript;
 
     public void PlayerDeath()
     {
         timerScript.ResetTimer();
     }
 }
avatar image Vasov97 Pengocat · Jan 26, 2017 at 05:37 PM 0
Share

Thanks a lot :)

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

92 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

Related Questions

Count Down Timer 1 Answer

timer stopwatch 2 Answers

Saving and displaying best time per level. 1 Answer

Disable Ragdoll #C - Timer before disable. 2 Answers

How to record lap time for player and AIs? 0 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