How to make a timer that counts up in seconds, as an int?
Im working on a simple game that relies on your total time in SECONDS ONLY to do some stuff. but Time.time gives me the value, but as a float. i just need a way to get it as an int. or a way to convert it. heres what i have so far:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UserScoreTracker : MonoBehaviour {
//the amount of time it took to finish the level. its the TIME, but its called score.
public int score = 0; // <-- NEEDS TO BE INT!!!!!
private bool Aktive; //heh lol "Aktive" XD
//the username input box/and where to store it and also waht to do with it.
public string username;
public GameObject ScoreUI;
public void UserInput(InputField userField)
{
username = userField.text;
}
public void Update()
{
score = Time.time;
}
public void SubmitSkcoar()
{
Highscores.AddNewHighscore(username,score);
}
public void OnTriggerEnter()
{
ScoreUI.SetActive(true);
}
}
Can someone help me?
Answer by darthtelle · Aug 31, 2015 at 10:52 AM
You still want the timer to count as a float, because that is what time is. However, when you get the value out of your score (time) float you can convert it into int seconds.
float timer = 0.0f;
void Update()
{
timer += Time.deltaTime;
int seconds = timer % 60;
}
Thanks for the help, but it still says it cant convert "type int with type float" but i tested, and it does give a whole value, but it isnt actually an integer; unity still stores it as a float, otherwise it throws errors. The reason i need this to work is because im making a scoreboard that is based of $$anonymous$$imal time in a race, but the scoreboard wont display float values. ive tried everything, but i just ended up just using whole values. :P
@Brosilio_Ga$$anonymous$$g No problem. We all have to start somewhere! $$anonymous$$ake sure to keep on track with maths and watch the Unity Live Training videos. They are tee$$anonymous$$g with useful tidbits!
@darthtelle thats true. Thanks for the help anyway!
Answer by rm-square · Mar 16, 2020 at 08:46 PM
As people come and read, i like to add another solution for a counter. The advantage in this solution is, that the Coroutine doesn't run in the Update method and won' be called every single frame.
This code will increase your score(time needed) variable every second by 1.
public int score = 0;
public void Start(){
StartCoroutine(time());
}
IEnumerator time(){
while (true)
{
timeCount();
yield return new WaitForSeconds(1);
}
}
void timeCount(){
score += 1;
}
When the level is finished and you put your score in the scoreboard, you should stop the counter with
StopCoroutine(time());
Nope, that one is depending on the fps. It might work for seconds, but not for milliseconds.
Answer by Content1of_akind · Apr 14, 2019 at 01:57 AM
if your C# compiler doesn't allow ---> seconds = Convert.ToInt32( timer % 60) or ---> int seconds = timer % 60; which mine does not for some reason, you can use this code instead to convert a float to an int. I'm using Visual Studio 2017 C# compiler
public float timer = 0.0f;
public int seconds;
void Update()
{
// seconds in float
timer += Time.deltaTime;
// turn seconds in float to int
seconds = (int)(timer % 60);
print(seconds);
}
Answer by Cibum · Apr 19, 2020 at 10:57 AM
Why not just
void Update()
{
timer = Time.time;
}
?
The documentation doesn't recommend it: "Regular (per frame) calls should be avoided: Time.time is intended to supply the length of time the application has been running for, and not the time per frame."
Answer by Stefan93 · Aug 19, 2017 at 08:26 AM
Now the code is complete...
float timer = 0.0f; int seconds ;
void Update() { timer += Time.deltaTime; seconds = Convert.ToInt32( timer % 60); // you can use the seconds wherever you wish }
Your answer
Follow this Question
Related Questions
Resetting scene time after 13 seconds 1 Answer
I make a timing score script in unity 2D, how i make high score of time?? Script is as follow: 1 Answer
Add x amount in t seconds? 2 Answers
Get position of object that uses Perlin Noise X distance/time back/offset? 0 Answers
Change scene after event (time or clicking on object) 0 Answers