Detect MouseClick on 2D Sprite (from a seperate Gameobject)

Hi,

I have a Game Manager script of sorts, and I am looking for a way to detect a OnMouseDown() event on number of UI GameObjects. The Game Manager script is attached to its own seperate Gameobject.

I have tags set for each UI element but I’m unsure how I would put this into code.
I’m using Sprites for my UI objects and so Raycasts apparently don’t work here :confused:

I hope you can help,
thanks :slight_smile:

Nevermind, I managed to fix my code (I’m not sure how), but it works fine now :slight_smile:
You can close this thread.

Sorry.

This is the code I used:

void checkMouseClick()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

            if (hit != null && hit.collider != null)
            {
                switch (hit.collider.tag)
                {
                    case "P1_Card0":
                        Debug.Log("You click Card 1");
                        break;
                    case "P1_Card1":
                        Debug.Log("You click Card 2");
                        break;
                    case "P1_Card2":
                        Debug.Log("You click Card 3");
                        break;
                    case "P1_Card3":
                        Debug.Log("You click Card 4");
                        break;
                }
            }
        }
    }