• 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 /
This question was closed Aug 15, 2015 at 09:00 AM by Flickayy for the following reason:

The question is answered, right answer was accepted

This post has been wikified, any user with enough reputation can edit it.
avatar image
27
Question by Flickayy · Aug 24, 2014 at 03:40 AM · c#uidynamic

[4.6 - UI] Changing the text component via script?

Hi all, I hope someone in the community can help me on this.

I've been using 4.6 for awhile now, mainly testing out the new UI system. However, I can't seem to find any good documentation on how to change any of the components via script.

Does somebody know how I can change the Text component's text? I've placed it inside the canvas and added a script that has a gameobject variable. Set that in the editor as the Text gameobject but can't seem to see any relative information in VS2013's intellisense.

Many thanks.

Comment
Add comment · Show 5
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 Spataar · Aug 24, 2014 at 09:56 AM 2
Share

Yeah, would love the answer to this aswell.

avatar image Kiwasi · Nov 20, 2014 at 08:00 PM 1
Share

Since this has made it back to the front page I'll put a link to the documentation here.

http://docs.unity3d.com/460/Documentation/$$anonymous$$anual/

http://docs.unity3d.com/460/Documentation/ScriptReference/

avatar image Michio-Magic · Feb 22, 2015 at 05:29 AM 0
Share

I just tried all your solutions too.... I am using Unity Script. "The name 'Text' does not denote a valid type ('not found'). Did you mean 'UnityEngine.UI.Text'?

I had to do declare it as UI.Text to make it work >>

var buttonText: UI.Text;

// ==================//

then

buttonText.text="46";

avatar image ocimum · Jul 21, 2015 at 03:37 PM 2
Share

add "using UnityEngine.UI;" in your header and everything should be accessible, thanks for your answer, otherwise I still would be searching for the solution! :)

avatar image Vintehin ocimum · Apr 11, 2016 at 07:30 PM 0
Share

Why are you not in first position ! ?

Thx for this answer ^^

6 Replies

  • Sort: 
avatar image
52
Best Answer

Answer by mlabarca · Aug 24, 2014 at 02:14 PM

Hi, First you would need to add this import: using UnityEngine.UI; Then, you can declare a Text field and later use getComponent to initialize it;

 Text instruction;

 void Start () {
    instruction = GetComponent<Text>();
 }

You would then be able to reference the instruction.text property and change it's string to whatever you like, or any other properties. Of course, this would be a script attached to a gameobject with the text field.

I found this while taking a look at ShowSliderValue.cs which is included with the UI example project provided by Unity this week.

Comment
Add comment · Show 7 · 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 Flickayy · Aug 24, 2014 at 10:34 PM 1
Share

Thanks, I know how to access components. Hopefully this question will help others. :)

avatar image infinitypbr · Aug 28, 2014 at 07:23 AM 7
Share

For Javascript:

 GetComponent(UI.Text)
avatar image TheDDestroyer12 · Nov 20, 2014 at 07:50 PM 0
Share

What is the UI Panel's class name?

avatar image hendy · Dec 06, 2014 at 09:19 AM 1
Share

Thank you. Currently the manual has no links to switch from Text's manual to Scripting, and neither is the other way around. Probably a documentation bug/oversight on Unity's part.

avatar image skyx26 · Feb 08, 2015 at 07:58 PM 1
Share

@mlabarca I have 2 questions regarding your awesome answer:

  1. What if you have more than one line of text?, how do you edit one especific line of text?, I.E: $$anonymous$$ENU

CONTINUE NEW OPTIONS QUIT

  1. How do you edit the color of the text?

Show more comments
avatar image
26

Answer by Nick4 · Aug 24, 2014 at 10:02 AM

You'll need to use UnityEngine.UI namespace, so add this on top of your script :

 using UnityEngine.UI
Comment
Add comment · Show 3 · 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 dude4004 · Apr 14, 2015 at 03:00 PM 1
Share

Thanks lot!

avatar image dasparadoxon · Jun 02, 2015 at 05:50 PM 1
Share

thanks for this small peace of information ! XD

avatar image ocimum · Jul 21, 2015 at 03:32 PM 0
Share

This is exactly what I was looking for half an hour! There are a lot of tutorials where GUIText is still used. $$anonymous$$ost of the time the snippets you can find for ui elements are also using GUIText. Im wondering, the UI package is not imported automatically if using UnityEngine is already included in the header.

avatar image
13

Answer by HVgame · Aug 24, 2014 at 02:40 PM

first

using UnityEngine.UI;

and

public Text Bro;

void Update () { Bro.text = "Get Back!"; }

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
12

Answer by 0mr_ashutosh0 · Nov 14, 2014 at 12:33 PM

IMPORTANT: This is attached to the text component of my button in UI canvas

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class DisplayScore : MonoBehaviour {
 
     Text txt;
     private int currentscore=0;
 
     // Use this for initialization
     void Start () {
         txt = gameObject.GetComponent<Text>(); 
         txt.text="Score : " + currentscore;
     }
     
     // Update is called once per frame
     void Update () {
         txt.text="Score : " + currentscore;  
         currentscore = PlayerPrefs.GetInt("TOTALSCORE"); 
         PlayerPrefs.SetInt("SHOWSTARTSCORE",currentscore); 
     }
 }
 
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 alsharefeeee · Mar 09, 2016 at 10:39 AM 0
Share

Thanks $$anonymous$$y friend

avatar image
0

Answer by tvranjith · Mar 13, 2015 at 06:23 PM

How unity implements the UI is just like any other unity components. You add GameObject and attach UI components to it. Once you have the game object you can get the UI components from it and change the properties.

API reference for UI can be found under UnityEngine.UI namespace, API reference for BUtton is http://docs.unity3d.com/ScriptReference/UI.Button.html

See the post http://chikkooos.blogspot.jp/2015/03/new-ui-implementation-using-c-scripts.html for more information on accessing UI from C# scripts

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 ocimum · Jul 21, 2015 at 03:35 PM 0
Share

Correct. But even you are answering the question, the problem is, the Text object could not be accessed as long as using UnityEngine.UI; was not included in the header. Think most of the problem is solved by including the UI package.

  • 1
  • 2
  • ›

Follow this Question

Answers Answers and Comments

37 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 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 avatar image avatar image avatar image

Related Questions

Solutions for dynamic interface 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to animate RectTransform change in position 1 Answer

Options UI over all scenes 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