- Home /
Input 3D text with script?
I have a 3D text object called "Menu_Text", and I'm trying to get it to change when the mouse moves over a plane. This is what I have:
function OnMouseEnter () {
var textMesh : TextMesh = GameObject.GetComponent("Menu_Text");
textMesh.text = "Blah";
}
I get this error:
Assets/Scripts/NewBehaviourScript.js(2,44): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'GetComponent'.
I don't understand?
EDIT
var myClip : AudioClip;
var Message : String;
function OnMouseEnter() {
audio.PlayOneShot(myClip);
var Thing : TextMesh = GetComponent("TextMesh");
Thing.text = Message;
}
This is the script as of now.
Answer by StephanK · Jun 04, 2011 at 05:54 AM
You are trying to access the function GetComponent() from the class GameObject. As this is not a static function you need an object of this class to call this function. In short: Instead of writing GameObject.GetComponent you can just write GetComponent().
I already tried that, but bizarrely it doesn't do a thing. It doesn't even display an error.
The other thing that is not correct is that you are supposed to pass the name of the Component/Type you want to access to GetComponent. So to get a Text$$anonymous$$esh component you will have to call GetComponent("Text$$anonymous$$esh")
It still isn't working or showing an error. I thought that the name of the object had to go in the quotes. In this case, "$$anonymous$$enu_Text".
No it's the name of the type. In this case Text$$anonymous$$esh.
Are you sure the On$$anonymous$$ouseEnter function gets called? Your Text$$anonymous$$esh object needs a collider for this.
The function is definitely being called. It plays a sound in the same function, and that's working perfectly. Could this be a problem with the font I'm using? Something with the texture or material?
Your answer
Follow this Question
Related Questions
Gameobject: 3D Text, how to change text? 2 Answers
Change 3D text priority 1 Answer
Center TextMesh to 3DModel 1 Answer
3D Text Mesh produces wrong letters 1 Answer
3d Text Drop shadow 2 Answers