I’m trying to show some text in a formatted way, so I figured that the best way is to dynamically create text boxes and put them in a grid.
So my logic is that I should create a gameObject, convert it to a text object, and then add it to the parent (the object that has grid layout component).
My code is as follows:
`
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GridTest : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject newObject = new GameObject("myText");
newObject.transform.parent = transform;
Text myText = newObject.AddComponent<Text>();
myText.text = "Hello!";
}
}
`
GridTest is a script attached to an image, where I added a grid layout component. The problem is that when I run the code, the text box doesn’t appear, I expect it to appear inside the image like when adding the text box manually as a child of the image.