The UI text that I’ve added is not rendering on my screen, and I cannot figure out why.
I followed the exact same steps as I’m using to dynamically add a UI image.
In the scene at run time, the RectTransform appears where it should (in the middle of the screen canvas) - but the text that it says has been set in the inspector is not showing.
Here is my code:
private Text SetHealthUiText(Canvas canvas)
{
var uiObject = new GameObject(Unit.Name + "HealthUi");
var rectTransform = uiObject.AddComponent<RectTransform>();
rectTransform.SetParent(canvas.transform);
rectTransform.localScale = Vector3.one;
rectTransform.localPosition = Vector3.zero;
rectTransform.position = Vector3.zero;
rectTransform.anchoredPosition = Vector2.zero;
rectTransform.sizeDelta = new Vector2(50, 50);
var text = uiObject.AddComponent<Text>();
text.font = new Font("Arial");
text.fontSize = 14;
text.color = Color.black;
text.text = "0";
return text;
}
What am I missing?