When click on UI, it also affects game control (UI and Input System)

Hello everyone, I’m making a tower stack game and currently I’m on the UI part.

The main function of the game is when you click anywhere on screen, it will drop the box. However, when I click on the Setting Button, it also triggers the OnFire() method from Default Input Actions Asset (New Input System of Unity).

I only use one Input Action Asset and have 2 separated Action Maps.

I searched on Google and found that EventSystem.current.IsPointerOverGameObject() could solve the problem but it will throw a warning and I think this is not good at all:

Calling IsPointerOverGameObject() from within event processing (such as from InputAction callbacks) will not work as expected; it will query UI state from the last frame
UnityEngine.EventSystems.EventSystem:IsPointerOverGameObject ()
BoxControl:OnFire () (at Assets/Scripts/BoxControl.cs:68)
UnityEngine.InputSystem.LowLevel.NativeInputRuntime/<>c__DisplayClass7_0:<set_onUpdate>b__0 (UnityEngineInternal.Input.NativeInputUpdateType,UnityEngineInternal.Input.NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate (UnityEngineInternal.Input.NativeInputUpdateType,intptr)

Script to Control Claw Movement:

    void MoveHorizontally()
    {   
        if(gameManager.GetIsGameOver() || soundSetting.activeSelf) {return;}
        
        position_x = transform.position.x + clawHalfSize * Mathf.Sign(moveSpeed); // Real x position
        if( position_x >= TopRightBounds.x - 1.25f || position_x <= BottomLeftBounds.x + 1.25f)
        {
            moveSpeed = -moveSpeed;
        }   
        moveSpeed = Random.Range(gameManager.GetMinSpeed(), gameManager.GetMaxSpeed() + 1) * Mathf.Sign(moveSpeed);
        transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
    }

Script to Control Box Movement / Functions:

    void DropBox()
    {
        // bug at Eventsystems...
        if(gameManager.GetIsGameOver() || UIControl.SoundSettingIsActive()) {return;}
        
        clawAnimator.SetBool("isGrabbing", false);
        myRigidBody.gravityScale = customGravityScale;  
        
    }

    void FollowClaw()
    {
        if(gameManager.GetIsGameOver() || UIControl.SoundSettingIsActive()) {return;}
        if(dropClicked) {return;}

        clawPos = new Vector3(clawMovement.transform.position.x, clawMovement.transform.position.y - clawGap); 
        transform.position = clawPos;
    }

Script that receive the user’s input:

    void OnFire()
    {
         if (EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }
        DropBox();
        dropClicked = true;
    }

This a screenshot from my game:

Thank you for all your support.

Well, I have found this tutorial on Youtube and I think it is a pretty good practice. The code is quite hard to understand but I think it will be very helpful when we need to implement a Input Manager in the future.

Link: https://www.youtube.com/watch?v=OEmoMKAWVvY&list=PLcRSafycjWFcSXprzCHaXYU3_ciTPnCSV&index=1

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms