Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question i have problem with Event system

Discussion in 'Editor & General Support' started by unity_CCAE36CE1B6672327E36, Aug 28, 2023.

  1. unity_CCAE36CE1B6672327E36

    unity_CCAE36CE1B6672327E36

    Joined:
    Jul 27, 2023
    Posts:
    1
    I have a script that handles user interactions with a game object's box collider using raycasts. This script is executed in the Update() function of a MonoBehaviour. However, I've encountered an issue where a button placed in front of the object can unintentionally trigger the raycast interaction when the user clicks the button. I attempted to prevent the raycast from executing when the button is clicked, but Unity's onClick() event is executed after the Update() function. As a result, the script still considers the game object as clicked even if the button was touched
    this the code of the update on the gameObject:
    '''

    void Update()
    {
    if (blockTouch)
    {
    return;
    }
    if (Input.touchCount > 0)
    {
    Touch CurrentTouch = Input.GetTouch(0);

    if (CurrentTouch.phase ==TouchPhase.Began)
    {

    Ray ray = cam.ScreenPointToRay(CurrentTouch.position);

    if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hitInfo, maxDistance: 100.0f))
    {
    ///Some Logics
    }
    }
    }
    '''

    and this on the button click
    '''
    public void buttonclicked()
    {
    blockTouch = !blockTouch;
    }
    '''

    I'm seeking any advice or approaches to address this situation and thank you !!