Clicking other game objects activates button

HI there,

I’m currently working on a program that is using a Deck as a button to instantiate a set number of cards at a fixed position corresponding to where the Deck (Deck Button) is. I’m using a Raycast and OnMouseButtonDown to click on the Deck Button but the issue I’m having is that whenever I click on any other Game Object in the scene the Deck Button still continues to act as if it were being pressed. I’m wondering if there is a way to click on the Deck Button without having to Raycast? Or if there were another work around so that the Deck doesn’t deal cards when other Game Objects are being pressed.

public class UserInput : MonoBehaviour
{
    private Gameplay gameplay;

    // Start is called before the first frame update
    void Start()
    {
        gameplay = FindObjectOfType<Gameplay>();
    }

    // Update is called once per frame
    void Update()
    {
        MouseClick();
    }

    void MouseClick()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 9f))
            {
                print(hit.collider.name);
                gameplay.DealFromDeck();
            }
        }
    }

It doesn’t look like you’ve added any conditions for DealFromDeck(). You print hit.collider.name. You can make an if statement checking the collider name