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 trialThanitsak Leuangsupornpong
7,490 PointsWhy 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
231,248 PointsOne 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
7,490 PointsThanitsak Leuangsupornpong
7,490 PointsThank 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
7,490 PointsThanitsak Leuangsupornpong
7,490 PointsOh 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;
Steven Parker
231,248 PointsSteven Parker
231,248 PointsGood to hear you resolved your issue. And remember to choose a "best answer". :)