Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Why does my Button Click not work?

Discussion in 'UGUI & TextMesh Pro' started by faulknordonald, Oct 9, 2023.

  1. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    151
    So, I have this code:

    Code (CSharp):
    1. public void LeftArrowButton()
    2.     {
    3.         Move(Vector2Int.left);
    4.     }
    And it's set in the onClick event:

    upload_2023-10-9_17-22-45.png

    This is precisely how others do it. What might be the issue?

    Any help is greatly appreciated.
     
  2. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    151
    Nevermind. I think it's just not executing my Move function. I added a Debug.Log("Button was Clicked") and it worked. I think I need it to be called in my update, but only when the button is clicked, not every frame.
     
  3. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    151
    I had to do this:

    Update:
    Code (CSharp):
    1. if(leftArrowButtonClicked)
    2.         {
    3.             Move(Vector2Int.left);
    4.             leftArrowButtonClicked = false;
    5.         }
    Function:
    Code (CSharp):
    1. public void LeftArrowButton()
    2.     {
    3.         leftArrowButtonClicked = true;
    4.         Move(Vector2Int.left);
    5.     }
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,943
    FYI I think you probably posted this in the wrong UI forum.
     
  5. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    151
    Probably. I often can't find the right forum. There's only like 3 or 4 UI forums that I see.