- Home /
How Do You Use GUIText to Move Through Multiple Texts?
I am trying to enable and disable text several times through the OnGUI command. It's easier to illustrate through an example. I have four different gameobjects with different text: text1, text2, text3, and text4. What I am trying to do is that when you click the GUIbutton, it will disable text1 and enable text2. When you click the GUIbutton again, it will disable text2 and enable text3. Click it again, text3 will be disabled and text4 will be enabled. As my code stands now, it only goes from text1 to text2, but not from text2 to text3 when I attach the following script to the text gameObjects (same script attached to several gameObjects).
public GUIText previousText;
public GUIText targetText;
private GUIText target;
// Use this for initialization
void Start () {
previousText = GetComponent<GUIText> ();
target = targetText.GetComponent<GUIText> ();
}
// Update is called once per frame
void OnGUI() {
GUI.backgroundColor = Color.black;
if (GUI.Button (new Rect (Screen.width - 120, Screen.height - 115, 120, 120), "I am button")){
previousText.enabled = false;
target.enabled = true;
}
}
I do not know if this is possible or very complex when it would be easier to do it another way, like make each text a scene. Any help would be extremely appreciated in getting this code to work.
Answer by Positive7 · Jul 28, 2015 at 11:30 AM
Here is one of my older script it's with UI Text but you can get the idea using UnityEngine; using UnityEngine.UI;
public class test3 : MonoBehaviour
{
public Text[] text;
public static int i = 0;
void Start () {
text = GetComponentsInChildren<Text> ();
}
void OnGUI() {
GUI.backgroundColor = Color.black;
if (GUI.Button (new Rect (Screen.width - 120, Screen.height - 115, 60, 60), "Next")) {
if (i < (text.Length - 1)) {
Next ();
}
}
if (GUI.Button (new Rect (Screen.width -260, Screen.height - 115, 60, 60), "Prev")) {
if (i > 0) {
Prev ();
}
}
}
void Next(){
text[i+1].enabled = true;
text[i].enabled = false;
i++;
}
void Prev(){
text[i-1].enabled = true;
text[i].enabled = false;
i--;
}
}
Your answer
Follow this Question
Related Questions
How to make a or array of GUI.Button's? 1 Answer
How to fire gui button on mouse right release 2 Answers
Distribute terrain in zones 3 Answers
GUI.Button is acting funky. 2 Answers