Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by tubswitch · Mar 27, 2016 at 05:27 AM · uierrortextcomponentmultiple objects

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fugogugo · Apr 03, 2016 at 03:09 PM 0
Share

why would you create everything via script anyway?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

74 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges