- Home /
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 expectederror CS1502: The best overloaded method match for
UnityEngine.Component.GetComponent(System.Type)' has some invalid arguments > > error CS1503: Argument
#1' cannot convertobject' 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";
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 ;-)
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.
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";
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";
}
}
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
![](https://koobas.hobune.stream/wayback/20220613112229im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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