Creating multiple text components on a canvas
In order to create a simple Debug Menu, I have created a Canvas on an empty GameObject using C#. I am now trying to add text to the Canvas.
My code is as follows:
GameObject g = new GameObject();
Canvas canvas = g.AddComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
CanvasScaler cs = g.AddComponent<CanvasScaler>();
cs.scaleFactor = 30.0f;
cs.dynamicPixelsPerUnit = 60f;
Font Arial = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
Text t = g.AddComponent<Text>();
t.text = "Test String";
t.font = Arial;
t.fontSize = 12;
t.color = Color.green;
t.alignment = TextAnchor.UpperLeft;
Text t_two = g.AddComponent<Text>();
t_two.text = "Test String 2";
t_two.font = Arial;
t_two.fontSize = 12;
t_two.color = Color.green;
t_two.alignment = TextAnchor.UpperRight;
However, when attempting to run the game, I get the following error on the line "t_two.text = "Test String 2."
NullReferenceException: Object reference not set to an instance of an object
Commenting out the first Text component allows the second one to display, meaning that there must be some issue with two Text components being attached to a single GameObject. How can I add multiple text objects to a single GameObject in order to display a canvas with more than one word on it?
EDIT:
I was able to get it to display properly, albeit a bit clumsily.
//Create the parent Game Object and canvas
GameObject g = new GameObject();
Canvas canvas = g.AddComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
CanvasScaler cs = g.AddComponent<CanvasScaler>();
cs.scaleFactor = 30.0f;
cs.dynamicPixelsPerUnit = 60f;
//position the canvas in the top left
g.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 250.0f);
g.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 250.0f);
g.GetComponent<RectTransform>().anchoredPosition = new Vector3(-325f, 125f);
//get the font
Font Arial = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
//create a game object to store the first text component, and make it a child of the canvas.
//this places the object's transform at the center of the parent
GameObject text_Object = new GameObject("Text");
text_Object.transform.SetParent(g.transform);
float text_Object_rectSize_width = 10;
float text_Object_rectSize_height = 10;
float text_Object_trans_x = -75;
float text_Object_trans_y = 75;
//using a rect transform, position it in the top left
RectTransform text_Object_trans = text_Object.AddComponent<RectTransform>();
text_Object_trans.sizeDelta.Set(text_Object_rectSize_width, text_Object_rectSize_height);
text_Object_trans.anchoredPosition3D = new Vector3(0, 0, 0);
text_Object_trans.anchoredPosition = new Vector2(text_Object_trans_x, text_Object_trans_y);
text_Object_trans.localScale = new Vector3(1.0f, 1.0f, 1.0f);
text_Object_trans.localPosition.Set(0, 0, 0);
CanvasRenderer renderer = text_Object.AddComponent<CanvasRenderer>();
Text t = text_Object.AddComponent<Text>();
t.text = "Test String";
t.font = Arial;
t.fontSize = 12;
t.color = Color.green;
t.alignment = TextAnchor.UpperLeft;
//create a game object to store the second text component
GameObject text_two_Object = new GameObject("Text");
text_two_Object.transform.SetParent(g.transform);
float text_two_Object_rectSize_width = 10;
float text_two_Object_rectSize_height = 10;
float text_two_Object_trans_x = 75;
float text_two_Object_trans_y = 75;
//position it in the top right
RectTransform text_two_Object_trans = text_two_Object.AddComponent<RectTransform>();
text_two_Object_trans.sizeDelta.Set(text_two_Object_rectSize_width, text_two_Object_rectSize_height);
text_two_Object_trans.anchoredPosition3D = new Vector3(0, 0, 0);
text_two_Object_trans.anchoredPosition = new Vector2(text_two_Object_trans_x, text_two_Object_trans_y);
text_two_Object_trans.localScale = new Vector3(1.0f, 1.0f, 1.0f);
text_two_Object_trans.localPosition.Set(0, 0, 0);
CanvasRenderer renderer_two = text_two_Object.AddComponent<CanvasRenderer>();
Text t_two = text_two_Object.AddComponent<Text>();
t_two.text = "Test String 2";
t_two.font = Arial;
t_two.fontSize = 12;
t_two.color = Color.green;
t_two.alignment = TextAnchor.UpperRight;
Unfortunately, with this method you don't seem to be able to anchor a component to its parent, requiring you to manually position it.
Answer by pfreese · Apr 01, 2016 at 05:53 PM
You can't create multiple components of the same type on a game object. Create child game objects under the canvas, each with their own text component.
Your answer
Follow this Question
Related Questions
i cant put text into a ui text thru a c# script 0 Answers
Can't re-parent GameObject with Text(script) component. 0 Answers
In-game counter stops counting when game is built? 1 Answer
NullPointException Object not found, caused by a variable not accepting value? 0 Answers
How do I change UI button background AND button text on click(pressed)? 0 Answers