Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Why my if else doesn't work on the second time when I click button?

if ( GUI.Button ( new Rect ( Screen.width * 0.5f - 75f, Screen.height * 0.1f, Screen.width * 0.4f, Screen.height * 0.1f), pause, "") ) {

            if (pause == pause) {

                Time.timeScale = 0;
                pause = unpause;
            }
            else if (pause == unpause) {

                Time.timeScale = 1;
                pause = pause;
            }

This is c sharp code, when I click first time game stop and change picture to un pause, but second time it not work. Please help.

1 Answer

Steven Parker
Steven Parker
231,248 Points

One obvious issue is that the following test:

            if (pause == pause) {

...is always true, so the else following it will never happen.

I'm not sure what exercise (or course!) this is from, but on a wild guess it looks like you intended to compare a local variable to some externally defined values named pause and unpause. So perhaps renaming it (in this example to status) might be in order:

// also rename the local variable both before and after the following test...
if ( GUI.Button ( new Rect ( Screen.width * 0.5f - 75f, Screen.height * 0.1f, Screen.width * 0.4f, Screen.height * 0.1f), status, "") ) {
            if (status == pause) {
                Time.timeScale = 0;
                status = unpause;
            }
            else if (status == unpause) {
                Time.timeScale = 1;
                status = pause;
            }

However, if my guess is not right, I'd need some more information to propose an effective solution.

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Thank you for the answer I already made other variable, I call it main. The problem is when I use main, I need to assign the picture from the pause to main, and make pause static. There no picture appear when static. Please help!

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Oh I got it! I solve the problem by this True False, and your suggestion. So when I click the button it will change the picture. public Texture pause; public Texture unpause;

    public Texture main;


    public bool a = true;

    if ( a == true ) { main = pause; }
    if ( GUI.Button ( new Rect ( Screen.width * 0.5f, Screen.height * 0.1f, Screen.width * 0.4f, Screen.height * 0.1f), main, "") || Input.GetKeyUp (KeyCode.P)) {

        if (main == pause) {


            Time.timeScale = 0;
            a = false;
            main = unpause;

        }
        else if (main == unpause) {

            Time.timeScale = 1;
            a = true;
            main = pause;

        }


    }
Steven Parker
Steven Parker
231,248 Points

Good to hear you resolved your issue. And remember to choose a "best answer". :)