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
1
Question by MikezNesh · Jul 15, 2010 at 06:51 PM · textgetcomponentcomponent

How to Get TextMesh component from the Gameobject the script is attached to?

I have a gameObject with a script attached to it. This gameObject has text attached to it. I want to be able to access this text in the script. How would I do this.

Please dont tell me to use GameObject.Find because that defeats the purpose because I will attach this script to many prefabs that all have 3D Text's. So then I would need to make lots of scripts.

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

3 Replies

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

Answer by StephanK · Jul 15, 2010 at 07:12 PM

If you can't use GameObject.Find you will have to save a reference to the GameObject or the TextMesh component somewhere. You can do this either by using public variables in your script and assign them throug the editor or if you have lots of them you should have some sort of manager that has a list or array of TextMeshes.

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 MikezNesh · Jul 15, 2010 at 07:23 PM 0
Share

I'm looking to make a game like Phage Wars. How would that game get access to all the texts when collided? Link: http://armorgames.com/play/2675/phage-wars

avatar image
1

Answer by Poemind · Sep 24, 2012 at 03:45 PM

Do you mean that there is no way of accessing a TextMesh component if it is a child of another Object? Because I need to do this as well, and the concept of doing this innumerable times is kind of crazy.

Comment
Add comment · 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
1

Answer by McMinty · Feb 13, 2014 at 03:53 AM

well... it may be a little late coming, but I have been working with somthing similar and thought I would answer anyway. it kinda depends on weather you want to set the text when you clone the prefab or from within the prefab's script.

From the script that clones the prefab try somthing like this:

 #pragma strict
 
 var TextObject: Rigidbody;
 var TCol : Color;
 
 function Start () {
 CloneTextObject ();
 }
 
 function CloneTextObject () {
 
 var TextOut : Rigidbody = Instantiate(TextObject, transform.position, transform.rotation);
         
         var TouT = TextOut.GetComponent(TextMesh);
         TouT.color = TCol;
         
         TouT.text = "YAY Text!";
 }


in this script the 3d text object has a Rigidbody componant since i wanted the text to move (fall) when spawned.

from inside the object itself it would be simpler just to set the text in the editer for the mostpart but if you were trying to get it to play a dialog or somthing try this:

 #pragma strict
 
 var TCol : Color;
 
 function Start () {
 TextOut ();
 }
 
 function TextOut () {
     var TouT = GetComponent(TextMesh);
     TouT.color = TCol;
     TouT.text = "YAY Text!";
 }

and either way, don't forget to set the color in the inspector or leave out the color bits to leave it default.

hope this helps.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Reference Not Set toInstance of Object 1 Answer

Multiple Text of canvas with One script ?? 1 Answer

Simple question about OnMouseButtonAsUp 1 Answer

Accessing script variables of instantiaded prefab 1 Answer

Can't change the 'text' of a 3DText object through a script, no matter what I do. 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