Im doing a Countdown on Debug. and it works but the numbers repeat.
I would like that only appears 1 time. .
using UnityEngine;
using System.Collections;
public class CuentaAtras : MonoBehaviour {
float currentTime = 0f;
float StartingTime = 10f;
void Start () {
currentTime = StartingTime;
}
void Update()
{
currentTime -= Time.deltaTime;
Debug.Log(currentTime.ToString("f0"));
if (currentTime <= 0)
{
currentTime = 0;
Debug.Log(currentTime);
Debug.Log("Se acabo el tiempo");
enabled = false;
}
}
}
Answer by highpockets · Apr 22, 2019 at 11:13 PM
It is repeating because it is a float, but you have formatted it to show just the whole number without the decimals. The actual value is hidden, if you didn’t format it with ToString(“F0”); you would be getting decimals like 8.954, 8.606, etc.. If you want to do a countdown from 10 to 0 and you want to use a float to do so. Just check if the value is the same as the last frame and if it is, don’t use it:
using UnityEngine;
using System.Collections;
public class CuentaAtras : MonoBehaviour {
float currentTime = 0f;
float StartingTime = 10f;
string timeLastFrame = “0”;
void Start () {
currentTime = StartingTime;
}
void Update()
{
currentTime -= Time.deltaTime;
if(timeLastFrame != currentTime.ToString(“f0”)){
timeLastFrame = currentTime.ToString(“f0”);
Debug.Log(timeLastFrame);
}
if (currentTime <= 0)
{
currentTime = 0;
Debug.Log(currentTime);
Debug.Log("Se acabo el tiempo");
enabled = false;
}
}
}
Your answer
Follow this Question
Related Questions
Why does Debug.Log continue to execute from Start() and why does InvokeRepeating() not? 1 Answer
Applying Scripts held in a list to a game object 1 Answer
How to call an override function if it was overrode. If not, just call the parent's function 2 Answers
How to call an action from a script that is on the anonymous object you collide with? 1 Answer