Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Spacemarine658 · Mar 24, 2015 at 06:30 AM · c#unity 5timer

Timers independant of the game i.e set timers

Timers independant of the game i.e set timers, I've been looking everywhere for a tutorial on how to use timers like candy crush where it takes the time from the moment you lose the heart untill 30 minutes then you get it back even if you close the game or something but I can't find any clear ones help?

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

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by dr3th · Mar 24, 2015 at 06:54 AM

 System.DateTime.Now; //will give you the current time
 System.DateTime lastTimeSaved; // store the last time an action happened.(all hearts gone)
 
 //Then what you want to so to get 30 minutes you have to add it to the last time an action happened.(all hearts gone)
 System.DateTime OneHeartReplenished = lastTimeSaved.AddMinutes(30);
 
 // Finally in your update script you will poll constantly if OneHeartReplenished is due
 
 If(System.DateTime.Now>OneHeartReplenished)
 {
   //generate new heart
 }
 
Comment
Add comment · 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
0

Answer by CodeMasterMike · Mar 24, 2015 at 07:06 AM

If I want it to work even when you close down the game, I would make use of the DateTime functions.

(See DateTime documentation for more information)

First you need to store the "starting" date for when I want the timer to be activated:

 DateTime timeAtActivation = DateTime.Now;

Now, you need to store this value somehow, either in a file, server or in the registry where you can acces it at any time from the game, even though the game has been turned closed in between game sessions.

So when you want to calculate how much time that has passed since it was activated, you ned to compare the current time with the time when the timer was activated. Something like so:

 // Get your stored activation time first. And then the current time. 
 DateTime currentTime = DateTime.Now;
 
 TimeSpan TimeSpanValue = currentTime.Subtract(TheStoredActivatedTimeValue);
 double elapsedTimeInMins = TimeSpanValue.TotalMinutes;
 
 if(elapsedTimeInMins >= 30.0d)
    HasTimerReachedItsTimeLimit = true;

The TimeSpan variable can return different values. So take a look at the TimeSpan documentation for more information.

Good luck!

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 Spacemarine658 · Apr 02, 2015 at 01:11 AM 0
Share

I found a way to do it but i'm having some issues

avatar image
0

Answer by Spacemarine658 · Apr 02, 2015 at 01:33 PM

my timer keeps counting but I can't find out how to set another timer for the next death and when i die it continually counts down the lives which is useless for a game

public class PlayerController : MonoBehaviour

{

 //Character Motor Variables
 Transform _charBottom;
 float _moveSpeed = 10f;
 bool _onGround;
 bool _canDoAction;
 bool _canJump;
 float _jumpForce = 900f;
 BoxCollider2D _collider;
 SpriteRenderer _renderer;

 //Lives System Variables
 bool _gameOver = false;
 int playerHeartCount = 5;
 public bool livesinfinte = false;
 public bool dead = false;
 DateTime oldDate;
 DateTime currentDate;
 int minutes;
 
 void Start() 
 {
     //How player will get infinte lives if they pay for it
     if (livesinfinte == true) {
         Debug.Log ("Unlimited Lives");
     }
     //dead = true;

 }

 void Awake ()
 {
     _charBottom = transform.FindChild ("charBottom"); 
     _collider = transform.FindChild ("collider").GetComponent<BoxCollider2D> ();
     _renderer = transform.FindChild ("spriteRenderer").GetComponent<SpriteRenderer> ();
 }
 
 // Update is called once per frame
 void Update ()
 {
     _onGround = Physics2D.Linecast (transform.position, _charBottom.position, 1 << LayerMask.NameToLayer ("Ground")); //checks ground

     if (!Overwatch.OW.IsPaused)
     {
         if (Input.GetKeyDown (KeyCode.Space))
         {
             if (_onGround) // change for double jump
             {
                 _canJump = true;
             }
     
         }
         if (Input.GetKeyDown (KeyCode.LeftControl))
         {
             if (_onGround)
             {
                 _canDoAction = true;
             }
         }
         else
         {
             _canDoAction = false;
         }
     }

     transform.position = new Vector3 (-8, transform.position.y, transform.position.z);

     currentDate = System.DateTime.Now;
     minutes = currentDate.Minute - oldDate.Minute;
     if (dead == true)
     {
         OnDeath();
     }
     if (minutes >= 30)
     {
         playerHeartCount++;

     }

 }

 void FixedUpdate ()
 {
     if (_canJump)
     {
         jump ();
     }

 }

 void jump ()
 {
     //modify for multi jump
     GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, _jumpForce));
     _canJump = false;
 }

 public void KillPlayer ()
 {
     dead = true;
     //Application.LoadLevel (Application.loadedLevel);
 }

 public float MoveSpeed
 {
     get { return _moveSpeed;}
     set { _moveSpeed = value;}
 }

 public bool CanDoAction
 {
     get { return _canDoAction;}
 }

 public BoxCollider2D Collider
 {
     get { return _collider;}
 }

 public SpriteRenderer Renderer
 {
     get { return _renderer;}
 }

 void OnGUI() 
 { 
     GUI.Label (new Rect (10, 10, 100, 20), oldDate.ToString ());
     GUI.Label (new Rect (10, 20, 100, 20), currentDate.ToString ());
     GUI.Label (new Rect (10, 30, 100, 20), minutes.ToString ());
     GUI.Label (new Rect (10, 40, 100, 20), playerHeartCount.ToString ());
     if (_gameOver == true) {
         GUI.Label (new Rect (0, 0, 100, 20), "Game Over!");
         Overwatch.OW.IsPaused = true;
         GUI.Box(new Rect(600,200,200,190), "Respawn Menu");
         if(GUI.Button(new Rect(610,220,180,40), "Main Menu")) 
         {
             Application.LoadLevel (Application.loadedLevel);
         }
         if(GUI.Button(new Rect(610,260,180,40), "Retry")) 
         {
             Application.LoadLevel (Application.loadedLevel);
         }
     }
 }
 void OnDeath()
 {
     oldDate = System.DateTime.Now;
     //will respawn here if lives available
     playerHeartCount -= 1;
     dead = false;
     _gameOver = true;

 }

}

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to work a real life timer? 2 Answers

Multiple Cars not working 1 Answer

the countdown timer inside a spawn() and inside while loop not working 1 Answer

Distribute terrain in zones 3 Answers

Timer doesn't count 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