Changing many UI Text with UI Button clicked

I’m new about 4.6 ui style.

I can make the button do easy and specific action like change scene, change object, call object, destroy object.


Now I have a problem. I want to change list of ui text like this

  • Text1 (Shown and Enabled)
  • Text2 (Hide and Disabled)
  • Text3 (Hide and Disabled)

Above name are name object which I haven’t tagged yet.

And 2 Button : Previous and Next


Now I can change Text1 to Text2 by setActive but from on click setting I can do once only.

If I want to destroy first next and enable another button which do for second next, it’s may expensive.


What method I should do?

  1. Add array for gameobject name and setActive by (“Text”+i) = false, (“Text”+i+1) = true and looped

  2. Script controller which I can use with both button in once

  3. Use arraylist or enum ?

If you want to have a ‘toggle’ system where you swap which UItext is enabled, try this:

public void TextSwap(){
    Text1.SetActive(!Text1.enabled);
    Text2.SetActive(!Text2.enabled);
}

This will set the Active state of the object to opposite of what it already is, causing it to toggle.

Sorry if this isn’t what you mean, your description isn’t that clear. Maybe provide code if this isn’t helpful.

Below code is my hard try to answer my question

I do 2 methods for each button and use array to keep the text I want to show.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Test : MonoBehaviour {
	
	int index;
	
	string[] texts = new string[]{"ABC","DEF","G","HIJ"};

	Text word;


	void Start()
	{
     		int index = 0;
     		word = GameObject.Find ("Text").GetComponent<Text>();
		     word.text = texts[index];
		}

	public void next()//Void for Next Button
	{
		    if(index!=texts.Length){
			     index++;

		     	word.text = texts[index];
			     print(texts[index]);}

		if(index==texts.Length)
		{
			     index=texts.Length;
			print ("This is last word");
		}
	}

	public void previous()//Void for Previous button
	{
			if(index!=0){
				index--;

				word.text = texts[index];
				print(texts[index]);}
			if(index==0)
			{
				index=0;
				print ("This is first word");
			}
		}

}