Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 DryreL · Dec 20, 2020 at 11:06 PM · buttonscript.backnext

Unity UI How to make Multiple Clickable Next / Back Buttons?

Hi, I'm looking for buttons that can be clickable multiple times.

For example; 1. click on next button: runs script A 2. click on same next button: runs script B 3. click on same next button : runs script C

Here's my code, but it only works with next button. I also need to multiple clickable back button on this code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class NextBackButton: MonoBehaviour
 {
 
     public apPortrait portrait;
 
     private int currentOption = 0;
 
     [SerializeField] private Button previousButton;
     [SerializeField] private Button nextButton;
 
    public void RunCode0()
     {
         //Do something
     }
 
    public void RunCode1()
     {
         //Do something
     }
 
    public void RunCode2()
     {
         //Do something
     }
 
     public void RunCode3()
     {
         //Do something
     }
 
     private void Start()
     {
         // set the initial text of the button
         //setButtonText(currentOption);
 
         // add an event listener to look out for button clicks
         nextButton.onClick.AddListener(myButtonClick);
     }
 
     public void myButtonClick()
     {
         switch (currentOption)
         {
             case 0:
                 // run stuff for option 1
                 RunCode0();
 
                     // change the current option for the next click
                     currentOption = 1;
                 
 
                 // change the text on the button to be the next option
                 //setButtonText(currentOption);
 
                 break;
 
             case 1:
                 Debug.Log("Doing option 2 things");
 
                 RunCode1();
 
                 // change the current option for the next click
                     
 
                 if(nextButton.interactable)
                 {
                     currentOption = 2;
                 }
                 else if(previousButton.interactable)
                 {
                     currentOption = 0;
                 }
                 
                 //setButtonText(currentOption);
                 break;
 
             case 2:
                 Debug.Log("Doing option 3 things");
 
                 RunCode2();
 
                 // change the current option for the next click
                     
                 if (nextButton.interactable)
                 {
                     currentOption = 3;
                 }
                 else if (previousButton.interactable)
                 {
                     currentOption = 1;
                 }
 
                 //setButtonText(currentOption);
                 break;
 
             case 3:
                 Debug.Log("Doing option 4 things");
                 RunCode3();
 
                 // change the current option for the next click
                 if (nextButton.interactable)
                 {
                     currentOption = 4;
                 }
                 else if (previousButton.interactable)
                 {
                     currentOption = 2;
                 }
 
                 //setButtonText(currentOption);
                 break;
         }
 
     }
 
     void setButtonText(string buttonText)
     {
         nextButton.transform.GetChild(0).GetComponent<Text>().text = buttonText;
     }
 }
 

Source: https://answers.unity.com/questions/1759036/how-to-use-button-multiple-times.html

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Hellium · Dec 21, 2020 at 01:21 AM

What some terrible code... Here is a complete rework I can suggest you. It involves more classes, but is way more extensible and manageable that what you have right now.

CODE NOT TESTED


NextBackButton.cs

 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class NextBackButton: MonoBehaviour
 {
     [SerializeField] private Button previousButton;
     [SerializeField] private Button nextButton;
     [SerializeField] private Text previousButtonLabel;
     [SerializeField] private Text nextButtonLabel;
     private int currentOption = 0;
     private ButtonListener[] listeners = new ButtonListener[]
     {
         new Foo(), new Bar(), new Baz("hello world")
     };
 
     private void Start()
     {
         SetListeners(0);
         previousButton.onClick.AddListener(() => SetListeners(currentOption - 1));
         nextButton.onClick.AddListener(() => SetListeners(currentOption + 1));
     }
 
     void SetListeners(int index)
     {
         listeners[currentOption].RemoveAsListener();
         listeners[(currentOption - 1 + listeners.Length) % listeners.Length].RemoveAsListener();
 
         currentOption = (index + listeners.Length) % listeners.Length;
 
         previousButton.interactable = currentOption > 0;
         if (previousButton.interactable)
         {
             listeners[(currentOption - 1 + listeners.Length) % listeners.Length].AddAsListener(previousButton);
             listeners[( currentOption - 1 + listeners.Length ) % listeners.Length].SetLabel(previousButtonLabel);
         }
         else
         {
             previousButtonLabel.text = "";
         }
 
         nextButton.interactable = currentOption < listeners.Length - 1;
         if (nextButton.interactable)
         {
             listeners[currentOption].AddAsListener(nextButton);
             listeners[currentOption].SetLabel(nextButtonLabel);
         }
         else
         {
             nextButtonLabel.text = "";
         }
     }
 }


ButtonListener.cs

 public abstract class ButtonListener
 {
     protected Button button;
 
     public abstract void SetLabel(Text text);
 
     public virtual void AddAsListener(Button button)
     {
         this.button = button;
         button.onClick.AddListener(PerformAction);
     }
 
     public virtual void RemoveAsListener()
     {
         if(button == null) return;
         button.onClick.RemoveListener(PerformAction);
     }
 
     protected abstract void PerformAction();
 }

Foo.cs = your RunCode0 function

 public class Foo : ButtonListener
 {
     public override void SetLabel(Text text)
     {
         text.text = "Foo";
     }
 
     protected override void PerformAction()
     {
         Debug.Log("Hey, I'm foo!");
     }
 }

Bar.cs = your RunCode1 function

 public class Bar : ButtonListener
 {
     private int selectedCount = 0;
 
     public override void SetLabel(Text text)
     {
         text.text = "Bar";
     }
 
     public override void AddAsListener(Button button)
     {
         base.AddAsListener(button);
         Debug.Log("Listener added"); // Just for the sake of the example, you don't need the function
     }
 
     protected override void PerformAction()
     {
         selectedCount++;
         Debug.Log("This option has been clicked " + selectedCount + " times");
     }
 }

Baz.cs = your RunCode2 function

 public class Baz : ButtonListener
 {
     private string input;
     public Baz(string input)
     {
         this.input = input;
     }
 
     public override void SetLabel(Text text)
     {
         text.text = input;
     }
 
     protected override void PerformAction()
     {
         Debug.Log(input);
     }
 }
Comment
Add comment · Show 1 · 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 DryreL · Dec 21, 2020 at 08:42 AM 0
Share

Hello, thanks for the code. I tested it and next button works fine but it's unlimited. Does not stop when it's come to end. And previous button works a bit, then gives error.

alt text

image-2020-12-21-114104.png (60.9 kB)
avatar image
0

Answer by CmdrZin · Dec 21, 2020 at 12:43 AM

Change your myButtonClick() to myButtonClick(int n). When in the On Click() for the Button, when you pick NextBackButton > myButtonClick you'll see it request an int value. Set this to 1 for your forwards button and to -1 for your back button and use it to adjust a counter.
Use the counter in myButtonClick to keep track of thing and test the count so that it doesn't go below 0 or greater than 3.

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 DryreL · Dec 21, 2020 at 09:12 AM 0
Share

Hello, thanks for your suggestion. I tried something like this but it gives error that "n does not exist in the current context"

alt text

image-2020-12-21-121158.png (25.6 kB)
avatar image CmdrZin · Dec 21, 2020 at 06:24 PM 0
Share

You don't need to use AddListener. LInk to myButtonClick in the Inspector using the OnClick () box.

avatar image
0

Answer by DryreL · Dec 21, 2020 at 11:50 AM

Finally, this works. Here's my code;

NextBackButton.cs

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  
  public class NextBackButton: MonoBehaviour
  {
      [Range(0, 3)] public int currentOption = 0;
  
      [SerializeField] private Button previousButton;
      [SerializeField] private Button nextButton;
  
      private void Start()
      {
          // set the initial text of the button
          //setButtonText(currentOption);
  
          // add an event listener to look out for button clicks
         previousButton.onClick.AddListener(() => myButtonClick(currentOption--));
         nextButton.onClick.AddListener(() => myButtonClick(currentOption++));
      }
 
      private void Update()
      {
         // Minimum Value
         if (currentOption < 0)
         {
             currentOption = 0;
         }
 
         // Maximum Value
         if (currentOption > 3)
         {
             currentOption = 3;
         }
      }
  
      public void myButtonClick(int optionValue)
      {
          switch (currentOption)
          {
              case 0:
                  // run stuff for option 1
                  RunCode0();
  
                  // change the text on the button to be the next option
                  optionValue++;
 
                  //setButtonText(currentOption);
                  break;
  
              case 1:
                  Debug.Log("Doing option 2 things");
                  RunCode1();
  
                  // change the text on the button to be the next option
                  optionValue++;
                  
                  //setButtonText(currentOption);
                  break;
  
              case 2:
                  Debug.Log("Doing option 3 things");
                  RunCode2();
  
                   // change the text on the button to be the next option
                  optionValue++;
 
                  //setButtonText(currentOption);
                  break;
  
              case 3:
                  Debug.Log("Doing option 4 things");
                  RunCode3();
 
                  // change the text on the button to be the next option
                  optionValue++;
 
                  //setButtonText(currentOption);
                  break;
          }
  
      }
  
      void setButtonText(string buttonText)
      {
          previousButton.transform.GetChild(0).GetComponent<Text>().text = buttonText;
          nextButton.transform.GetChild(0).GetComponent<Text>().text = buttonText;
      }
 
      public void RunCode0()
      {
          //Do something
      }
  
     public void RunCode1()
      {
          //Do something
      }
  
     public void RunCode2()
      {
          //Do something
      }
  
      public void RunCode3()
      {
          //Do something
      }
  }
Comment
Add comment · 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

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

157 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 avatar image avatar image avatar image

Related Questions

Please help with playing music for button click 2 Answers

How to control an AudioSource with an AudioClip? 2 Answers

Draw with GL by script 0 Answers

Texture2d Array with Next Button 1 Answer

Go Back Button Script 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