Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Pom Pom · Apr 14, 2015 at 08:38 PM · unity 5scripting problemscripting beginner

How to change Texts in Unity 5 using UI Texts?

Hello,

I'm doing the Roll a Ball Tutorial in Unity 5. Until the "Displaying Text" part it went pretty well but i can't find any solution to change the text displayed by UI Texts with a script... (As i'm very new to Unity i'd like to know how to make basic things like this work in Unity 5 and not using GUIText as it is said in the tutorial).

I dug a lot in the documentation, on the forums and on Unity answers but i never managed to do it.

In the inspector it says Text (Script) so i assumed it was a script and searched how to change script variables from other scripts, it didn't helped a lot so...

Here are the things (highlighted in yellow) i'm trying to interact with: alt text

here is my script (attached to the Player game object) as it is right now and i think that the only thing missing in it is the damn method i've been searching everywhere these last three days.

 using UnityEngine;
 using System.Collections;
 
 public class characterController : MonoBehaviour {
     public float speed;
     public float sprint;
     private int count = 0;
     private string Text;
     public Component scriptText; 
 
     void Start(){
         count = 0;
         scriptText = GameObject.Find ("/Canvas/CountText").GetComponent ("Text (Script)");
         setCountText ();
 
     }
     void FixedUpdate(){
 
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         sprint = Input.GetAxis ("Sprint");
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         if (sprint!=0) {
             speed = 1500;
             GetComponent<Rigidbody> ().AddForce (movement * speed * Time.deltaTime);
             speed = 500;
         }
         else{
             GetComponent<Rigidbody> ().AddForce (movement * speed * Time.deltaTime);
         }
     }
 
     void OnTriggerEnter(Collider other){
         if (other.gameObject.tag == "pickup") {
             other.gameObject.SetActive (false);
             count++;
             setCountText();
         }
     }
     void setCountText(){
         string counts = count.ToString ();
         Text = "Count: " + counts;
         // change the text in the UI script Text
     }
 
 }
 
capture.png (220.0 kB)
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
7
Best Answer

Answer by DoTA_KAMIKADzE · Apr 14, 2015 at 08:39 PM

You need to store a reference to that Text component.

 private Text txtRef;
 private void Awake()
 {
     txtRef = GetComponent<Text>();//or provide from somewhere else (e.g. if you want via find GameObject.Find("CountText").GetComponent<Text>();)
 }
 //then where you need:
 txtRef.text = "text";

Or you can make it public/[SerializeField] and assign it by drag&drop in Inspector view.

 public Text txtRef;
 //or
 [SerializeField]
 private Text txtRef;
 //and then the same thing - where you need it:
 txtRef.text = "text";
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 Pom Pom · Apr 15, 2015 at 11:33 PM 0
Share

I tried the first solution and got that error:

Assets/Scripts/characterController.cs(8,17): error CS0246: The type or namespace name `Text' could not be found. Are you missing a using directive or an assembly reference?

(which means i should have the same problem with the second solution) is there any kind of library that i need to put with a "using" at the beginning of the code?

avatar image DoTA_KAMIKADzE · Apr 16, 2015 at 01:06 AM 7
Share

using UnityEngine.UI;

P.S. Though if you don't want, you can always call it like:

UnityEngine.UI.Text ins$$anonymous$$d of Text

avatar image st362 DoTA_KAMIKADzE · Aug 11, 2017 at 07:57 PM 0
Share

thank you! UnityEngine.UI.Text did the trick

avatar image Pom Pom · Apr 16, 2015 at 01:54 PM 0
Share

Works great, thanks a lot!

avatar image Not_Sure · Aug 30, 2015 at 02:03 PM 1
Share

I just spent hours pulling my hair out trying to figure out why this wasn't working.

THAN$$anonymous$$ YOU!!!

avatar image Narden · Apr 27, 2017 at 10:54 AM 0
Share

An object reference is required to access non-static member `UnityEngine.GameObject.GetComponent()' can you solve this :(

avatar image st362 Narden · Aug 11, 2017 at 08:01 PM 0
Share

Your using get component? How?

You can't use GetComponent() like your used to anymore with Unity > version 4: See the reference below:

Unity no longer uses “Helper References” to access common components.In Unity 5 and later we can no longer access components using their “shorthand helper references” and we must access them directly using “GetComponent”.One example of this is accessing the Rigidbody component attached to the same GameObject as the script. In Unity 4 and earlier, this was simply accessed with “rigidbody.”Now this must be done with “GetComponent().” It is usually a “best practice” to find this Component when the instance of the script initializes, and “cache” the reference in a local variable.This is commonly written as:

private Rigidbody rb;

void Start (){ rb = GetComponent(); }

Now you can access rb.velocity or whatever you need.

Here's a link to where it is: https://oc.unity3d.com/index.php/s/rna2inqWBBysn6l?_ga=2.186647084.508043714.1502467811-1075521444.1501799035

avatar image
0

Answer by taktaz208 · Aug 16, 2017 at 01:18 AM

Use at the top of the script:

 using UnityEngine.UI;

Then write beside other publics:

 Public Text test;

Then Where you want to change:

 test.text = "YOUR TEXT";

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

8 People are following this question.

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

Related Questions

Sprite dots spawn and have their own color, but they only show as wireframes? 0 Answers

Game is not restarting after gameOver=true 1 Answer

"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer

When my player dies and i hit revive button i want my player back to the game but i m unable to do that please help. 2 Answers

How can i count the number of times i look to the transform.rotation.y < -0.2,How to count the amount of times i look down? 0 Answers


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