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
1
Question by lnwhitling · Apr 10, 2020 at 11:34 AM · uirenderingtextdynamically

Dynamically added UI text is not displaying

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?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by lnwhitling · Apr 12, 2020 at 08:19 AM

Ok - I finally got to the bottom of this.

Specifying the font is necessary - and of course I was doing that incorrectly.

The fault in my code was: text.font = new Font("Arial");

It should have been: text.font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");

In the inspector, when I paused the scene, it showed Arial as being the font, so I assumed that this wasn't a problem. Egg on my face.

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
avatar image
0

Answer by streeetwalker · Apr 10, 2020 at 11:46 AM

@lnwhitling, If you see the text you set in the inspector and you are satisfied it is in the location where you should see it, but it does not show on the screen, then it could be the width and height of the Text element is not big enough.

Try increase the width to like 200 and maybe the height. You can fiddle with those values in the inspector while the game is running to find the right values. (or any of the other values if changing the size doesn't fix it. )

Comment
Add comment · Show 3 · 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
avatar image lnwhitling · Apr 10, 2020 at 11:56 AM 0
Share

Hmm, the object is definitely appearing in the scene. And the inspector is definitely showing expected values. And the rect transform is where I would expect it.

I've changed the width and height of the rect transform substantially (is this what you meant by increasing width and height?), changed the text length, and upped the font size, but no joy.

Is there a possibility that the text is rendering outside of the rect transform?

avatar image streeetwalker lnwhitling · Apr 12, 2020 at 08:26 AM 0
Share

If a text element's width or height is not big enough to accommodate the string length at a given font size, I have consistently noticed the element doesn't display anything on the screen.

$$anonymous$$ake the font size smaller, and the width larger to see if that is the problem.

You can test it yourself directly in the editor: create a UI text element and put some text into the Text component in the inspector. Then change the size of the width and height until the text disappears. Or leave them the same and increase the font size until it disappears.

avatar image lnwhitling streeetwalker · Apr 12, 2020 at 08:30 AM 0
Share

Yea - I played around with all that stuff. But because I wasn't correctly specifying the font, it wasn't rendering it.

Having seen it work, I can happily say that I'm going to immediately abandon this in favour of using a Text$$anonymous$$eshPro element ins$$anonymous$$d.

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

300 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

UI Text component is not rendering correctly on MAC (sub pixel issue) 0 Answers

UI Text sometimes renders when instantiated, yet sometimes does not. 1 Answer

Best way to create dynamic text objects with Canvas? 0 Answers

Score UI per player 1 Answer

Can not properly display the score on pong game 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