UI change Canvas Panel with buttons right or left

Hi, I have problem with getting two buttons to work on my menu. Buttons are left and right arrows that should change the panel on the canvas. buttons are making sound and when panel reach the edge of canvas left or right button.setActive is turn on. I got this to working with “Fire2” as left and “Jump” as right.
How would i get my buttons instead of “Fire2” and “Jump”?

Code looks like this:

void Update() {
        ShowLeftButton();
        ShowRightButton();

        if (Input.GetButtonDown("Fire2"))  // press alt to -2000f   SHOULD BE "SlideLeft"   for now is Fire2 which is alt
        {
            if (slidePanel.anchoredPosition == new Vector2(5453.0f, -365.0f) ||
                    slidePanel.anchoredPosition == new Vector2(3453.0f, -365.0f) ||
                    slidePanel.anchoredPosition == new Vector2(1453.0f, -365.0f) ||
                    slidePanel.anchoredPosition == new Vector2(-547.0f, -365.0f))
            {
                slidePanel.anchoredPosition = slidePanel.anchoredPosition + slideLeftAmount;
            }
        }
        if (Input.GetButtonDown("Jump"))  // press space to +2000f    SHOULD BE "SlideRight"   for now is Jump which is space
        {
            if (slidePanel.anchoredPosition == new Vector2(-2547.0f, -365.0f) ||
                    slidePanel.anchoredPosition == new Vector2(-547.0f, -365.0f) ||
                    slidePanel.anchoredPosition == new Vector2(1453.0f, -365.0f) ||
                    slidePanel.anchoredPosition == new Vector2(3453.0f, -365.0f))
            {
                slidePanel.anchoredPosition = slidePanel.anchoredPosition + slideRightAmount;
            }
        }
    }

Thank You.

Do you mean UI buttons or do you mean different buttons, as in Input.GetButton ?

Yes its UI buttons.

Okay, so for UI buttons, what you want to do is:
First, create 2 methods (1 for each of the code you have now with GetButton), and make them public.
Then, remove the Update code about the buttons you have now.
Next, add an event to each button (OnClick event → little plus sign)
Drag the game object with the script onto the game object of the OnClick event.
Use the dropdown menu for the On-Click event, and select the method :slight_smile: Repeat that for the other button.

1 Like

Thanks, this is what I came up with, and its working as I wanted.
Here is the code if someone else needs it.

    void Start()
    {
        buttonLeft = GameObject.FindGameObjectWithTag("SlideLeft").GetComponent<Button>();
        buttonRight = GameObject.FindGameObjectWithTag("SlideRight").GetComponent<Button>();
      
        buttonLeft.onClick.AddListener(LeftClick);
        buttonRight.onClick.AddListener(RightClick);

        slidePanel = GetComponent<RectTransform>();
        slidePanel.anchoredPosition = new Vector2(5453.0f, -365.0f);  // 1. 5453 // 2. 3453  // 3. 1453  // 4. -547  // 5. -2547
    }

    private void LeftClick()
    {
        if (slidePanel.anchoredPosition == new Vector2(-2547.0f, -365.0f) ||
            slidePanel.anchoredPosition == new Vector2(-547.0f, -365.0f) ||
            slidePanel.anchoredPosition == new Vector2(1453.0f, -365.0f) ||
            slidePanel.anchoredPosition == new Vector2(3453.0f, -365.0f))
        {
            slidePanel.anchoredPosition = slidePanel.anchoredPosition + slideRightAmount;
        }
    }

    private void RightClick()
    {
        if (slidePanel.anchoredPosition == new Vector2(5453.0f, -365.0f) ||
            slidePanel.anchoredPosition == new Vector2(3453.0f, -365.0f) ||
            slidePanel.anchoredPosition == new Vector2(1453.0f, -365.0f) ||
            slidePanel.anchoredPosition == new Vector2(-547.0f, -365.0f))
        {
            slidePanel.anchoredPosition = slidePanel.anchoredPosition + slideLeftAmount;
        }
    }

    // Update is called once per frame
    void Update()
    {
        UpdateButtons();
    }

Cool - glad you got it working. You went the code route vs the inspector, but it still worked out. :slight_smile:

Please refer to this page for future posts on the forums: Using code tags properly
It shows you how to insert code so it’s nicer to read.

1 Like

Thank You Methos5k very good tip with the code indentation. :slight_smile: