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 /
avatar image
0
Question by Ephesiel · Aug 29, 2018 at 04:01 PM · text

Text doesn't appear when i add it via script

Hey !

My work is very simple : I want to create a score text wich will change during my game. But when I add the text via a c# script, the text doesn't appear (the gameObject is here, just the text isn't). In the inspector, the text appear, but not in the scene.

When i have a text already in my scene, it's appear

     GameObject score = new GameObject("Score");
     score.transform.SetParent(gameObject.transform);

     RectTransform rt = score.GetComponent<RectTransform>();
     rt.sizeDelta = new Vector2(150, 50);
     rt.anchoredPosition = new Vector2(0, 0);

     score.AddComponent<Text>().text = "Score = 0";

PS : i'm working on a UI element

alt text

sans-titre.png (117.7 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MacDx · Aug 29, 2018 at 04:26 PM

I think the problem is that UI components like Text, need to be attached to a game object that has a RectTransform instead of a regular Transform in order to render. Calling new GameObject() like that will create a game object with a regular transform so that won't work.


Try this instead

 GameObject score = new GameObject("Score", typeof(RectTransform));

This should create a GO with the right transform.


Don't forget that game objects with UI components need to be children of a Canvas object. If the parent of this new GO isn't part of a hierarchy where a Canvas is at the top, it won't work.


Note: Also check things like font size, color and such to see if the text is just hidden.


Hope this helps!

Comment
Add comment · Show 8 · 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 Ephesiel · Aug 29, 2018 at 04:34 PM 0
Share

No, it's not working either :/

Yes, the text is children of a canvas, and the parent is the canvas itself I didn't put size, color, font, etc... in the script, but i put pause and change the values, it change nothing. The gameObject with the text composant is here, I can see the text in the text section (it's "Score = 0" as expected) but I can't see it in the scene

avatar image MacDx Ephesiel · Aug 29, 2018 at 04:36 PM 0
Share

Color's alpha is not 0 right? If you could post a screenshot or give some more info about the setup, it would help find the problem

avatar image MacDx Ephesiel · Aug 29, 2018 at 04:41 PM 0
Share

Did you also assign a font through script?

avatar image MacDx · Aug 29, 2018 at 04:44 PM 0
Share

There's the issue! No font assigned, also, you won't see white text on a white background

avatar image Ephesiel · Aug 29, 2018 at 04:45 PM 0
Share

Updated with a picture (there is no font, but if i add one, it change nothing)

avatar image MacDx Ephesiel · Aug 29, 2018 at 04:49 PM 0
Share

So, you put a font and also changed the color to something other than white and it still didn't work?

avatar image Ephesiel MacDx · Aug 29, 2018 at 06:01 PM 0
Share

Yep, it's that

avatar image Ephesiel · Aug 30, 2018 at 10:11 PM 0
Share

So ? Do you know a solution ?

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

91 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

Related Questions

A text apears on the screen when you enter a trigger 1 Answer

Gui text component on an object not showing up? 0 Answers

Having proper text areas in unity? 0 Answers

I cant read the file i wrote, unless i reload it in VS. 1 Answer

Can TextMesh support multiline text? 1 Answer


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