- Home /
Two buttons behaving entirely differently (despite similar logic in code)?
I'm trying to create an "Instructions" window so that the player will know how to play the game. The instructions consist of several Text UIs. These will act as the several pages of the instructions.
I have 5 Text UIs called page_1, page_2, page_3, page_4 and page_5. I created 2 buttons "Next" and "Back" so that the player can navigate through the pages ("Next" should take you to the next page of the instructions while "Back should take you to the previous page"). However, I've noticed that the "Back" button works perfectly fine while the "Next" button takes me directly to page_5 (no matter what page I'm in right now). For example, if I'm in page_1 and I want to go to page_2, I just have to hit the "Next" button. But when I do that, it takes me to page_5 instead. It behaves this way for all the pages.
Here's how I coded the events for the 2 buttons:
public void onNext()
{
if (page_1.gameObject.activeSelf)
{
page_2.gameObject.SetActive(true);
page_1.gameObject.SetActive(false);
}
if (page_2.gameObject.activeSelf)
{
page_3.gameObject.SetActive(true);
page_2.gameObject.SetActive(false);
}
if (page_3.gameObject.activeSelf)
{
page_4.gameObject.SetActive(true);
page_3.gameObject.SetActive(false);
}
if (page_4.gameObject.activeSelf)
{
page_5.gameObject.SetActive(true);
page_4.gameObject.SetActive(false);
nextButton.gameObject.SetActive(false);
}
}
public void onBack()
{
if (page_1.gameObject.activeSelf)
{
//The code here just closes the instructions panel and returns to the main menu screen. It works fine.
}
if (page_2.gameObject.activeSelf)
{
page_1.gameObject.SetActive(true);
page_2.gameObject.SetActive(false);
}
if (page_3.gameObject.activeSelf)
{
page_2.gameObject.SetActive(true);
page_3.gameObject.SetActive(false);
}
if (page_4.gameObject.activeSelf)
{
page_3.gameObject.SetActive(true);
page_4.gameObject.SetActive(false);
}
if (page_5.gameObject.activeSelf)
{
nextButton.gameObject.SetActive(true);
page_4.gameObject.SetActive(true);
page_5.gameObject.SetActive(false);
}
}
And then I just connect the script to the default OnClick() section for each button in the inspector and set the desired functions.
As you can see, the onBack() function follows the exact same logic as the onNext() function. But for some reason, the "Next" button just directly takes me to page_5 every time. But the "Back" button works as expected.
I used to boolean variables to find out what's happening behind the scenes. And it looks like whenever I click the "Next" button, the onClick() event is triggered multiple times, hence taking me to page_5. But why is it only happening for the "Next" button?
Answer by Vicarian · Jul 08, 2018 at 01:59 AM
Utilize else if. The way your code is working now, it chains from the first case to the last. For instance, if you start on page_1, the first if block passes, deactivates page_1, then activates page_2. The very next block checks for page_2 being active, so it passes, and so on.
I understand but why isn't the same thing happening for the onBack()function? The onBack() function seems to be working perfectly even though I used the same logic in coding.
It's a simple matter of order. If you'd reverse the order of the if blocks in the back function, the exact same thing would happen. Code doesn't execute backwards.