In my game, I want a box with the scores of each player to show up at the end of a level. The box is easy since I can just drag a UI Image into the scene, but because the number of players changes and I haven’t decided on a maximum yet, I want to generate the text through code. I created a Text prefab and thought I had some code that would work, but the text doesn’t show up on screen:
GameObject go = (GameObject)Instantiate (Resources.Load ("Prefabs/UIText"));
go.GetComponent<RectTransform> ().position = new Vector3 (Screen.width * 0.5f, Screen.height * 0.5f, 0.2f);
go.GetComponent<Text>().text = str;
The problem can’t be that str is empty, because I check that it isn’t with Debug.Log right before this. It can’t be that it shows up behind the box, because the box is transparent. And it’s definitely showing up at the right position- I checked with Debug.Log right after this code. So what’s going on?