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

Button OnClick not work that I think

Discussion in 'UGUI & TextMesh Pro' started by Przemek_S, Jun 15, 2015.

  1. Przemek_S

    Przemek_S

    Joined:
    Aug 18, 2014
    Posts:
    9
    Hello,

    I have player object with this script:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         startPos = gameObject.transform.position;
    4.         if ((Input.GetKeyDown (KeyCode.RightArrow) || MobileRight == true) && gameObject.transform.position == endPos && gameObject.transform.position.x <= 2)
    5.         {
    6.             endPos = new Vector3 (transform.position.x + 1, transform.position.y, transform.position.z);
    7.         }
    8.         if ((Input.GetKeyDown (KeyCode.LeftArrow) || MobileLeft == true) && gameObject.transform.position == endPos && gameObject.transform.position.x >= -2)
    9.         {
    10.             endPos = new Vector3 (transform.position.x - 1, transform.position.y, transform.position.z);
    11.         }
    12.  
    13.         gameObject.transform.position = Vector3.Lerp (startPos, endPos, perc);
    14.     }
    If on Keybord I pressed and hold Left or Right Arrow my player run only one in left or right direction(transform position +/- 1).
    When I create button and click this my player run to the max in left or right direction.

    I try OnClick, PointerDown with Pointer Up and PointerClick with this code
    Code (CSharp):
    1.     public void Prawy()
    2.     {
    3.         MobileRight = true;
    4.         MobileLeft = false;
    5.     }
    6.     public void Lewy()
    7.     {
    8.         MobileLeft = true;
    9.         MobileRight = false;
    10.     }
    Code (CSharp):
    1.     public void Prawy(bool Pbool)
    2.     {
    3.         MobileRight = Pbool;
    4.         MobileLeft = false;
    5.     }
    6.     public void Lewy(bool Lbool)
    7.     {
    8.         MobileLeft = Lbool;
    9.         MobileRight = false;
    10.     }
    If somebody have any idea how I make button click script I will be grateful for help.
     
  2. LyveIG

    LyveIG

    Joined:
    Jan 8, 2015
    Posts:
    15
    Input.GetKeyDown returns true only once, this means that your code is only executed once, therefore the jump +/-1.

    But you never reset your MobileRight/MobileLeft flags, therefore the code is executed every frame. What you need to add is MobileLeft = MobileRight = false at the end of your update method.
     
  3. LyveIG

    LyveIG

    Joined:
    Jan 8, 2015
    Posts:
    15
    Even better would be to move the code into a function and call it only once in your OnClick method of your button.