Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 FunExplosion · Feb 14, 2014 at 07:16 PM · c#inspectorcomponenttext mesh

I'm having trouble getting the text in Unity's "text mesh" to change its text at runtime.

 public class ShipPropulsion : MonoBehaviour
 {
     bool onGround = false;
 
     int mph = 90;
 
         void Update()
     {
         GameObject speedometer = GameObject.Find ("Speedometer");
         string readSpeed = speedometer.GetComponent<TextMesh>().text;
         readSpeed = mph.ToString();
 
     }
 }

I have attempted this a few different ways, and I'm not getting any errors, but I'm also not getting anything else. The value just stays displayed as "50" as I've set it in the inspector. I've tried substituting the int with a string, and still get no change in the display at runtime.

I'm sure it's something simple.

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
0
Best Answer

Answer by robertbu · Feb 14, 2014 at 07:19 PM

In need to actually change the object referenced by the TextMesh.text. So instead do:

 speedometer.GetComponent<TextMesh>().text = mph.ToString();
Comment
Add comment · Show 4 · 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 FunExplosion · Feb 14, 2014 at 07:34 PM 0
Share

Hmm. It worked, but I don't understand why. Shouldn't the way I wrote it have worked? What is the difference between what I wrote and the way you wrote it?

avatar image perchik · Feb 14, 2014 at 07:43 PM 1
Share

When you set readSpeed to equal the text, it copies the text into the new string variable but readspeed does not refer to the text object

That is, string readSpeed = ...().text; says to copy the value in ().text and save it to a new string called readSpeed

Then, when you change the value of readSpeed, the original object's .text method is not changed.

In @robertbu's method, he is actually changing the value of the .text parameter.


Now, if you wanted to do it your way, and have a variable point at the textmesh, this would work equally well:

 Text$$anonymous$$esh tm = speedometer.GetComponent<Text$$anonymous$$esh>() as Text$$anonymous$$esh;
 tm.text = mph.ToString();

avatar image robertbu · Feb 14, 2014 at 07:57 PM 1
Share

@perchik's comments are right on. A diagram.

alt text

Your code first sets 'readSpeed' to the old string (orange arrow), then it set it to the new string. Your code does not set Text$$anonymous$$esh.text to the new string. Strings are immutable in C#, so there is no easy way to change the contents of the old string. 'mph.ToString() produces a new string.

ref.png (12.7 kB)
avatar image FunExplosion · Feb 14, 2014 at 08:16 PM 0
Share

Very helpful replies, guys. That helped a lot.

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

19 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

Related Questions

Inspector vs Script: Component best practice? 1 Answer

Can you hide a custom icon for a script in the scene view? 0 Answers

Distribute terrain in zones 3 Answers

Where can I find a complete list of Attributes and how they work and 2 Answers

Unity can't see any scripts 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