Displaying C# Int Value to GUI
Hey all,
I'm starting a new project and I'm trying to create a date function where every seconds adds another day. So far, my code is:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class date : MonoBehaviour {
public int day;
void dayAdd () {
day++;
}
void Start () {
InvokeRepeating ("dayAdd", 0f, 1.0f);
}
}
Now I'm trying to show this int value to a UI. I haven't found any method that particularly works for me, so I would love some help!
Thanks!
Answer by UnityCoach · Jan 06, 2017 at 01:24 PM
You need to reference the TextComponent, like :
public Text uiText;
drag and drop the Text object in it in the inspector, then, in Update for example, you can simply do this :
uiText.text = day.ToString();
Answer by dpoly · Jan 06, 2017 at 04:46 AM
You simply need a Text component with a RectTransform and Canvas Renderer, and ultimately a Canvas somewhere in the parent tree.
You can place that easily anywhere in your UI, but attaching it to 2D game objects is a bit harder.
I have those all put in, but what do I use in my script to put the var into the UI text?
Your answer
Follow this Question
Related Questions
What is the most accurate way to call a function based on time? 0 Answers
Suspension of Physics 1 Answer
Time.time doesn't show up? 2 Answers
Problems with scripting and invokeRepeating 1 Answer
Hour Range - Do Something? 2 Answers