- Home /
Grabbing UI text as a public variable?
Hello, I am watching a tutorial for making a game in Unity and I'm having an issue with one of my scripts. The tutorial I'm watching uses the old GUI system instead of the new UI system, so I'm having a hard time replicating what the tutorial is looking for in the UI system. Here's the code that the tutorial uses, w/ the GUI system:
using System.Collections;
using UnityEngine;
public class Finish : MonoBehaviour {
public GUIText winText;
// Use this for initialization
void Awake ()
{
winText.enabled = false;
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Player")
{
winText.enabled = true;
}
}
}
How do I replicate the "public GUIText winText" with the UI system? Also, do I need to put "using UnityEngine.UI" at the top of this script? Thank you in advance!
Answer by Reynarz · Jul 29, 2017 at 06:56 PM
In the new Ui system just add the UnityEngine.Ui, and use the class called "Text":
using System.Collections;
using UnityEngine;
using UnityEngine.Ui;
public class Finish : MonoBehaviour
{
//Text Class.
public Text winText;
// Use this for initialization
void Awake ()
{
winText.enabled = false;
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Player")
{
winText.enabled = true;
}
}
}
Answer by oStaiko · Jul 29, 2017 at 06:54 PM
IIRC, to convert the two all you would need is to replace GUIText with Text, and yes add UnityEngine.UI to the top of the script. Also, if you want to change to text via script, you need to assign it through Text.text
Your answer
Follow this Question
Related Questions
Making texts appear/disappear after having completed a task 0 Answers
How to display a message from socket (python) on the screen (Unity) 0 Answers
Can't see text for menu in unity with a texture placed 1 Answer
How to attach a canvas to a game object properly? 1 Answer
Best practice for creating Canvas text in Javascript? 0 Answers