Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheSaviour · Jul 08, 2018 at 12:25 AM · uibuttonclickbutton trigger events

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheSaviour · Jul 08, 2018 at 12:40 PM 0
Share

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.

avatar image Vicarian TheSaviour · Jul 08, 2018 at 07:50 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

154 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Highlight images in buttons children 0 Answers

UI button in Panel not clickable 2 Answers

Detect UI Button Click Event in Update method 3 Answers

Lags when clicking so many times,Have problem when clicking so many times 1 Answer

Unity UI Button not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges