- Home /
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:
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
}
}
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";
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?
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
thank you! UnityEngine.UI.Text did the trick
I just spent hours pulling my hair out trying to figure out why this wasn't working.
THAN$$anonymous$$ YOU!!!
An object reference is required to access non-static member `UnityEngine.GameObject.GetComponent()' can you solve this :(
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
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";