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 KaiUwe · Aug 28, 2014 at 02:30 PM · uiunity 4.6

Unity 4.6 Change the content from a text Object through script

Hi, I have got the problem that I want to change a text GUI element through script in unity 4.6 for showing a score in my game but I do not get it to work because I dont know how to access the Text component. So if anyone could help me I would be rally happy :-)

P.S.: I'm sorry for my bad english

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

2 Replies

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

Answer by Landern · Aug 28, 2014 at 02:33 PM

You get it like anything else.

 // UnityScript
 import UnityEngine.UI;
 
 var someText: Text;
 
 function Start()
 {
   someText = GetComponent.<Text>(); //
 }
 
 function Update()
 {
   someText.text = "Hello and junk";
 }
 
 // c#
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class SomeScriptName : MonoBehaviour
 {
   public Text someText;
 
   void Start()
   {
     someText = GetComponent<Text>();
   }
 
   void Update()
   {
     someText.text = "Hello im some text";
   }
 }
Comment
Add comment · Show 9 · 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 KaiUwe · Aug 28, 2014 at 09:43 PM 0
Share

Ohh I see.

 import UnityEngine.UI;

that is what i have forgotten. Thanks a lot :-)

avatar image Wolfbane54 · Dec 23, 2014 at 08:15 AM 0
Share

Does this work with numbers?

avatar image shriya · Dec 23, 2014 at 08:42 AM 0
Share

yes it works with numbers

avatar image AcE_fLoOdEr · Jan 01, 2015 at 05:08 AM 1
Share

That's not gonna be efficient if the user has more than 1 text element in their canvas. It will end up changing all of them. Best thing to do is to do the following, it's hardcoded so :)

 void Start()
 {
    GameObject canvas = GameObject.Find("Canvas");
    Text[] textValue = canvas.GetComponentsInChildren<Text>();
    textValue[enter the index of the text object here].text = "hey";
 }

i would love to find out another way, this seems too sloppy.

avatar image Landern · Jan 01, 2015 at 05:44 AM 1
Share

@Ace_Flooder, it would not change all of them, it would get the first Component of type Text.

If you want to keep references to a multitude of objects, setup a Dictionary of type string and of type Text. Use the key by name to reference the reference value of the Text type and make your modifications to the text property as needed.

In the case you posted you would have to know the index by int. that would be sloppy, at least using a $$anonymous$$ey Value Pair type, you can associate logical key names with the object.

textValue[18] would require a look up for the programmer to remember what it is.

using the dictionary method, you would use something like

 textValue["scoretext"].text = scoreInt.ToString();

In the case above they were asking about a single component, you are conflating the issue.

Show more comments
avatar image
0

Answer by Blyler · Mar 09, 2015 at 07:34 AM

I wrote the following function, based on @Ace_Flooders excellent answer, for finding text elements by name using Linq:

 private Text GetTextObjectByName(string name)
 {
     var canvas = GameObject.Find("Canvas");
     var texts = canvas.GetComponentsInChildren<Text>();
     return texts.FirstOrDefault(textObject => textObject.name == name);
 }

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 4.6 UI - change image's source image via script 4 Answers

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

Unity 4.6 UI - set slider value via script 3 Answers

Avoiding click-though in 4.6 1 Answer

Unity 4.6 GUI select button via script 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