Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Jun 27, 2017 at 05:25 AM by hexagonius for the following reason:

The question is answered, right answer was accepted

avatar image
10
Question by davidflynn2 · Nov 20, 2012 at 07:38 PM · c#timer

Simple Timer

I am needing help geting started on creating a basic timer for my game I would like it to start with 60 seconds then count down to 0.

I know its problualy very basic but I have googled it and cant seem to find any for C#.

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

3 Replies

  • Sort: 
avatar image
24
Best Answer

Answer by GeorgeRigato · Nov 20, 2012 at 08:06 PM

Hi again


 using UnityEngine;
 using System.Collections;
 
 public class SimpleTimer: MonoBehaviour {
 
 public float targetTime = 60.0f;
 
 Update(){
 
 targetTime -= Time.deltaTime;
 
 if (targetTime <= 0.0f)
 {
    timerEnded();
 }
 
 }
 
 void timerEnded()
 {
    //do your stuff here.
 }
 
 
 }
 


Have fun.

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 ytNICC · Jun 27, 2017 at 04:59 AM 0
Share

Thx man! But, i have a simple problem... I need to show the time in some gui text or something like this... Can you help me? :)

avatar image
2

Answer by drearyplane · Oct 26, 2015 at 12:34 PM

@GeorgeRigato

I am using the exact same code for my latest project, but it isn't working. I am in Unity 5.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class PlayerMover : MonoBehaviour {
 
     public float speed;
     public Text countText;
     public Text winText;
     public float targetTime = 10.0f ;
 
     private Rigidbody rb;
     private int count;
 
 
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody>();
         winText.text = "";
     }
     
     // Update is called once per frame
     void Update () {
     
         targetTime -= Time.deltaTime;
 
         if (targetTime <= 0.0f) {
 
             timerEnded();
 
         }
 
 
     }
 
 
     void OnTriggerEnter(Collider other) {
         
         
         
         if (other.gameObject.CompareTag ("Pick up")) {
             
             other.gameObject.SetActive (false);
             count = count + 1;
             SetCountText();
 
         }
 
         if (count >= 10) {
 
             winText.text="You won!";
 
             Application.Quit();
 
         }
     }
 
 
     void FixedUpdate() {
         
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         
         rb.AddForce(movement * speed);
     }
 
     void SetCountText (){
         
         countText.text = "Memories: " + count.ToString ();
         
     }
 
     void timerEnded (){
 
         winText.text = "You Lost!";
 
             Application.Quit();
 
     }
 
 }

I have put the entire code just in case there is a problem somewhere else.

Comment
Add comment · Show 2 · 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 Bonfire-Boy · Oct 26, 2015 at 02:35 PM 0
Share

You haven't said what is happening when you run your code.

I would start by adding some logging code to check (for example) whether or not timerEnded() is being called.

And if you're testing in the editor, note that Application.Quit() will have no effect there. So your code would set the text to "You lost" (once per frame once the timer has reached zero) but carry on.

avatar image drearyplane Bonfire-Boy · Jul 15, 2017 at 03:36 PM 0
Share

It's a bit late by now (only two years late) but I did get this to work and I'm getting better at using these places after getting suspended from StackOverflowExchange halfway through a project with a deadline. Now it's quite obvious I should've used Debug.Log. I was very inexperienced at Unity back then.

avatar image
1

Answer by Democre · Nov 20, 2012 at 08:46 PM

If you want it to countdown, you could try something like this:

 public class Countdown: MonoBehaviour {
     public int duration = 60;
     public int timeRemaining;
     public bool isCountingDown = false;

     public void Begin()
     {
         if (!isCountingDown) {
             isCountingDown = true;
             timeRemaining = duration;
             Invoke ( "_tick", 1f );
         }
     }
 
     private _tick() {
         timeRemaining--;
         if(timeRemaining > 0) {
             Invoke ( "_tick", 1f );
         } else {
             isCountingDown = false;
         }
     }
 }

This will give you a public "timeRemaining" member that you can watch or display in another script. It also gives you a flag to watch to determine whether this object is actively counting down.

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 CaptainShrek · Apr 25, 2017 at 04:49 PM 0
Share

what would you attach the script too?

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Perform action If Input is done in this time. 2 Answers

How do I stop my timer by accessing a variable from another script? 2 Answers

Editable Timer 0 Answers

A method running each millisecond in real time 0 Answers

How can I add time to our timer when a target is destroyed? C# 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