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 axsw · Aug 14, 2013 at 08:59 PM · errortextchangetextmesh

error when trying to change TextMesh.text through script

Hey guys, I have a problem I can't understand.

In my scene there is a 3D Text object with a script assigned to it. The script is a simple MonoBehaviour script and looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class DisplayHighscore : MonoBehaviour
 {
     
     // Use this for initialization
     void Start()
     {
         GetComponent(TextMesh).text = "hi";
     }
 }

But I get these three error messages and I have no idea why.

error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

error CS1502: The best overloaded method match for UnityEngine.Component.GetComponent(System.Type)' has some invalid arguments > > error CS1503: Argument #1' cannot convert object' expression to type System.Type'

I hope one you knows what's going wrong here...

btw:

when I do it like this I don't get an error, but nothing happens:

 TextMesh text = (TextMesh)GetComponent(typeof(TextMesh));
 text.text = "hi";



Comment
Add comment · Show 2
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 axsw · Aug 15, 2013 at 02:24 PM 0
Share

Okay, one day later I open Unity, start my game and... It works! I didn't make any changes, but the code at the bottom seems to work now... Guess it happens from time to time.

Only one question left: why does the code above give me these error messages?

Anyway, thanks for your time ;-)

avatar image Sajidfarooq · Aug 15, 2013 at 04:39 PM 0
Share

I have answered this question, along with the previous, in a new answer below. Hopefully it answers your question so you can mark it as "answered" now.

2 Replies

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

Answer by Sajidfarooq · Aug 14, 2013 at 09:58 PM

I can confirm that your code works. Tested on Unity 4.2

 // Use this for initialization
 
     void Start () 
     {
     
         TextMesh text = (TextMesh)GetComponent(typeof(TextMesh));
         text.text = "hi";
     }

As for the Question of why the previous code doesn't work, its simple. This line is wrong:

 TextMesh text = GetComponent(TextMesh);

GetComponent expects a "type" as its parameters, whereas you are passing it an object. The correct way to do it would be this:

 GetComponent(typeof(TextMesh));

Also, the "return" value of this function is a generic object. In order to use it as a TextMesh, you must "cast" it into a TextMesh like this: (TextMesh)GetComponent...

So the final code looks like this:

 TextMesh text = (TextMesh)GetComponent(typeof(TextMesh));

Or even this:

( (TextMesh)GetComponent(typeof(TextMesh)) ).text = "hi";

Comment
Add comment · Show 1 · 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 axsw · Aug 16, 2013 at 01:12 PM 0
Share

Thanks for the explanation.

avatar image
0

Answer by CodeAssembler · Aug 14, 2013 at 09:23 PM

Should do it like this if the script is in the actual 3D Text object:

 using UnityEngine;
 using System.Collections;
 
 public class changeMe : MonoBehaviour {
     
     TextMesh my3dText;
     // Use this for initialization
     void Start () {
         my3dText = transform.GetComponent<TextMesh>();
     }
     
     // Update is called once per frame
     void Update () {
     
         my3dText.text = "Hi there dude";
     }
 }
Comment
Add comment · Show 1 · 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 Sajidfarooq · Aug 14, 2013 at 09:56 PM 1
Share

Your code should all be in "Start". Putting it in "Update" is not only unnecessary (only need to do it once), its slow.

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

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Cannot Import TextMeshPro Essentials 0 Answers

Change text gui.button in a for bucle 1 Answer

Text Mesh Scripting 1 Answer

Another way to reference scripts? 2 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