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 Lineweaver · Mar 22, 2019 at 04:00 AM · unity 4.6time.deltatimetimer-script

Increase a value by one every minute

I'm trying to increase a value by one every 60 seconds. This is what I got so far: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class DayCounter : MonoBehaviour {

 public Text secondsText;
 public Text dayText;
 int day;
 float seconds;

 void Start ()
 {       
     seconds = PlayerPrefs.GetFloat("Seconds Text", 0);
     secondsText.text = "Seconds: " + seconds.ToString("00");

     day = PlayerPrefs.GetInt("Day Text", 0);
     dayText.text = "Day: " + day;
 }
 
 void Update ()
 {
     seconds = (int)(Time.time % 60f);
     secondsText.text = "Seconds: " + seconds.ToString("00");
    
     if (seconds >= 5)
     {
         day++;
         seconds = 0;
         secondsText.text = "Seconds: " + seconds.ToString("00");            
         dayText.text = "Day: " + day;

         Debug.Log("a day has passed");
     }
 }

the 5 seconds mark and the seconds display are just for testing

The text display works fine. The problem is that after 5 seconds, the seconds counter gets stuck on zero, and the days counter keeps increasing forever.

I want a script that doesn't show the seconds display ingame, and increases the value of the Day variable by one every minute.

Any help would be appreciated ^^

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Lineweaver · Mar 22, 2019 at 06:31 PM

I figured out how to do it using this post: https://stackoverflow.com/questions/45653990/increase-variable-x-amount-every-x-seconds

The final script looks like this:

 public class DayCounter : MonoBehaviour {

 public Text secondsText;
 public Text dayText;
 int day;
 public int dayMark;
 float seconds;

 void Start ()
 {       
     seconds = PlayerPrefs.GetFloat("Seconds Text", 0);
     secondsText.text = "Seconds: " + seconds.ToString("00");

     day = PlayerPrefs.GetInt("Day Text", 0);
     dayText.text = "Day: " + day;
 }
 
 void Update ()
 {
     seconds += Time.deltaTime;
     secondsText.text = "Seconds: " + seconds.ToString("00");
     if(seconds >= dayMark)
     {
         DayCount();
         dayText.text = "Day: " + day;
         seconds = 0;
     }      
 }

 void DayCount()
 {
     day++;
 }

}

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
1

Answer by Cornelis-de-Jager · Mar 22, 2019 at 04:06 AM

Use Coroutines:

 public int day;
 
 void Start  () {
     day = 0;
     StartCoroutine(Timer(60));
 }
 
 IEnumerator Timer(int time) {
     yield return WaitForSeconds(time);
     day++;
     seconds = 0;
     secondsText.text = "Seconds: " + seconds.ToString("00");            
     dayText.text = "Day: " + day;
     Debug.Log("a day has passed");
 }
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 Lineweaver · Mar 22, 2019 at 02:22 PM 0
Share

I used part of your answer, now my script looks like this:

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

 public class DayCounter : $$anonymous$$onoBehaviour {

 public Text secondsText;
 public Text dayText;
 int day;
 float seconds;

 void Start ()
 {       
     seconds = PlayerPrefs.GetFloat("Seconds Text", 0);
     secondsText.text = "Seconds: " + seconds.ToString("00");

     day = PlayerPrefs.GetInt("Day Text", 0);
     dayText.text = "Day: " + day;

     StartCoroutine(Timer(5)); //5 seconds for testing
 }
 
 void Update ()
 {
     seconds = (int)(Time.time % 60f);
     secondsText.text = "Seconds: " + seconds.ToString("00");
   
 }

 IEnumerator Timer(int time)
 {
     yield return new WaitForSeconds(time);
     day++;
     seconds = 0;
     secondsText.text = "Seconds: " + seconds.ToString("00");
     dayText.text = "Day: " + day;
     Debug.Log("a day has passed");       
 }

}

After 5 seconds the day counter changes to one, but after that the counter doens't increase to 2. I tried activating the coroutine from the Update function, but then the day counter keeps increasing forever again. How can I make it so the counter changes every $$anonymous$$ute throughout the entire game?

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

169 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 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

the following Code after "yield return new WaitForSeconds();" not executed 1 Answer

Trigger a Timer 2 Answers

Farming game Time management 0 Answers

change timer float while still counting down 2 Answers

Milliseconds Timer Question 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