Search Unity

Unity UI UI button is set to pressed after scene change

Discussion in 'UGUI & TextMesh Pro' started by mathiasbc04, Dec 31, 2022.

  1. mathiasbc04

    mathiasbc04

    Joined:
    Dec 21, 2020
    Posts:
    33
    I have made a button that changes scene after holding it. But there is another button in the next scene in the same place. So when I release the cursor/finger the button in that next scene is pressed. How can I prevent this?



    This is the script for the hold button:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. public class HoldButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    7. {
    8.     private bool pointerDown;
    9.     private float pointerDownTimer;
    10.  
    11.     [SerializeField]
    12.     private float requiredHoldTime;
    13.  
    14.     public UnityEvent onLongClick;
    15.  
    16.     [SerializeField]
    17.     private Image fillImage;
    18.  
    19.     public void OnPointerDown(PointerEventData eventData)
    20.     {
    21.         pointerDown = true;
    22.         Debug.Log("OnPointerDown");
    23.     }
    24.  
    25.     public void OnPointerUp(PointerEventData eventData)
    26.     {
    27.         Reset();
    28.         Debug.Log("OnPointerUp");
    29.     }
    30.  
    31.     private void Update()
    32.     {
    33.         if (pointerDown)
    34.         {
    35.             pointerDownTimer += Time.deltaTime;
    36.             if (pointerDownTimer >= requiredHoldTime)
    37.             {
    38.                 if (onLongClick != null)
    39.                     onLongClick.Invoke();
    40.  
    41.                 Reset();
    42.             }
    43.             fillImage.fillAmount = pointerDownTimer / requiredHoldTime;
    44.         }
    45.     }
    46.  
    47.     private void Reset()
    48.     {
    49.         pointerDown = false;
    50.         pointerDownTimer = 0;
    51.         fillImage.fillAmount = pointerDownTimer / requiredHoldTime;
    52.     }
    53.  
    54. }
     
  2. mathiasbc04

    mathiasbc04

    Joined:
    Dec 21, 2020
    Posts:
    33
    I came up with an idea and it looks like it works. So before the hold button moves you to another scene it sets a PlayerPrefs to 1. When the button in the next scene is pressed it checks if that PlayerPrefs is not equal to 1. If it is not equal, it changes the scene like it should. However, if it is equal to 1, the script just sets the PlayerPref to 0
     
  3. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    If it works and you are happy that might be good enough but it has what is known as "code smell" :) One typically doesn't have to resort to tricks for common behaviors. In this case (I am guessing but I think) the fundamental problem is that the click handler is never being disabled.

    If you have two separate buttons (that appear aligned on top of each or not) they should have separate click handlers. As one button's lifetime expires it should remove the handler as it no longer needs to listen for events. And again (I'm guessing but so many people do this) you have assigned the event in the inspector. I recommend against that and this example is why.

    When your button needs to listen you add a handler when it is done you remove it. That won't happen when you use the inspector.