Question by 
               BasdeWildt · May 06, 2020 at 11:46 AM · 
                c#unity 2dmenueventsystemclick objects  
              
 
              Cant click on GamObject
So I'm trying to make a clickergame. i want a menu to open when my sprite is clicked. Righ now I've got a script that detects the click but it doesnt matter where I click, it just opens it.
 using UnityEngine.EventSystems;
 
 public class BuyMenuBB2 : MonoBehaviour, IPointerClickHandler
 {
     public GameObject buyMenu;
 
     public void OnPointerClick(PointerEventData eventData)
     {
         throw new System.NotImplementedException();
     }
 
     private void Update()
     {
         if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
         {
             OpenBuyMenu();
         }
     }
 
     public void OpenBuyMenu()
     {
         buyMenu.SetActive(true);
     }
     public void CloseBuyMenu()
     {
         buyMenu.SetActive(false);
     }
 }
 
               I have a button setup that closes the menu.
               Comment
              
 
               
              Answer by CSPAG · May 06, 2020 at 03:21 PM
Try using a raycast
 private void Update()
     {
         if (Input.GetMouseButtonDown(0))
         { 
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hit, 100.0f))
             {
                 if (hit.transform != null)
                 {
                     PrintName(hit.transform.gameObject);
                 }
             }
         }
     }
 
     private void PrintName(GameObject go)
     {
         print(go.name); //I just made the object print name here but you can do what you wish :)
     }
 
              i tried this but i'm using unity 2d and i can't figure out how it works
Hi there, sorry for the late reply, I don't have a lot of experience with 2D but I believe you should be able to attach the script to the camera.
With the script as it is now, you should be able to click on objects in-game, the object name should then appear in the console to check if it is working.
Your answer