making text appear and disappear with button click

I have a game where currently I have 60 scenes for 20 questions. How would I make this simple by just making the text change with one button click? Also, I am not a professional coder or anything so please dumb it down. Thanks.

The simplest way to make text appear or disappear is to simply enable or disable either the Text component or the GameObject it is attached to. Simple example below. With a UI Button you’d just add ToggleText() to the button’s OnClick event. If you don’t understand what that means, follow any basic UI tutorial and/or read the relevant section of the manual on setting up a UI, buttons, and text.

public Text DisplayedText;

public void ToggleText()
{
    if (DisplayedText.enabled == true)
    {
        DisplayedText.enabled = false;
    }
    else
    {
        DisplayedText.enabled = true;
    }
}

Note the logic above is usually written in a much simpler form, but I wrote it this way because it is easier to understand for a new coder.

Here a complete script, don’t forget to associate the Objects.

public class ShowHideText : MonoBehaviour
{
    public Text text;
    public Button button;
    private bool switchText = true;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    public void ChangeText(){

        switchText = !switchText;
       

        if (switchText){
            text.enabled = true;
        } 
        else {
            text.enabled = false;
        }
    }
}

Can I use this with images?

1 Like