I am currently developing a mobile game using Unity 5 that includes a timer. The timer is supposed to be updating every second.
I have a weird and stupid problem I cannot find the solution to. I'm hoping someone has encountered something like this before. I initially made the timer a GUI object, before I had Unity 5. It worked correctly when it was a GUIText. Basically, what is happening is in the inspector, the text component changes correctly. However, in realtime, in game view, the text does not change. I also already adapted my code from the GUIText to Text. So that is not the problem either. The snippets of my code are below:
private Text guiText;
void Start () {
guiText = gameObject.GetComponent<Text>();
guiText.text = timer.ToString();
}
void Update () {
timer += Time.deltaTime;
timerString = timerFormat(timer);
guiText.text = timerString;
}
string timerFormat(float currentTime)
{
TimeSpan ts = TimeSpan.FromSeconds(Mathf.Round(currentTime));
int hours = ts.Hours;
int minutes = ts.Minutes;
int seconds = ts.Seconds;
timerString = string.Format("{0:00}:{1:00}:{2:00}", hours, minutes, seconds);
return timerString;
}
}
According your screenshot your TextComponent is too small, so the texts are being truncated.
Here you have some options:
I suggest you to try the first option and check what happens with some border cases values (00:00:00, 59:59:59, 100:59:59, -100:59:59, etc.. besides the final (or normal) values that will be used on the TextComponent you have to assure that even with wrong values your TextComponent will act as expected.)
http://docs.unity3d.com/es/current/Manual/script-Text.html
Some video tutorials:
https://unity3d.com/es/learn/tutorials/modules/beginner/ui/ui-text https://unity3d.com/es/learn/tutorials/projects/roll-a-ball/displaying-text
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.