Change the name of a toolbar button by clicking it. How?

Hello,

how can I change the name of a toolbar button, when I clicking it?

For instance:

I have a button called “Pause”. If I´m clicking it, the game will freeze and the Button-Name is changing to “Resume”. If I´m clicking the Button “Resume”, the game will continue and the button-name is changing to “Pause”. And so on.

I know how to write a button with 2 functions (like pausing and resuming the game), but changing the name of the button? I really don´t know, how to do it.

Can someone help me?

Disclaimer: I’m assuming you’re using GUI.Button.

The solution to your problem is to send in the Button’s text as a variable rather than just writing it out. So it’d be something like:

string buttonText;
bool isPaused = false;

void Start() {
    buttonText = "pause";
}

void OnGUI() {
    if (GUI.Button(new Rect(...), buttonText)) {
        if(isPaused) {
            isPaused = false;
            buttonText = "pause";
        } else {
            isPaused = true;
            buttonText = "play";
        }
    }
}

That’ll make a button that switches text as you press it. Hope that helps!