2

I have such a question. I have some objects on the screen. These objects contain box collider. When I click on them something happens, it does not matter. Now I do not know how to do when I click somewhere on the screen but not on one of the objects, to give me Debug.Log("Bad Click, here is no object"); I was thinking of taking the cursor position and detecting if it is above an object and if not, but I do not understand.

public GameObject[] objects;

public void Start()
{
    foreach (GameObject ob in objects)
    {
        ob.GetOrAddComponent<ColliderEventSystem>().ColliderEntered += Click;
    }
}

private void Update()
{
    //Do something when I click wrong
}
Cenkisabi
  • 1,066
  • 8
  • 25

1 Answers1

2

One approach would be to use raycast. If the cast does not return a collider on click, you can print your statement / run your code.

Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;

        if (Physics.Raycast(Camera.main.ScreentoWorldPoint(Input.mousePosition), Vector3.forward, out hit))
            Debug.Log("object clicked: " + hit.collider.name));
        else
            Debug.Log("Bad Click, here is no object")
    }
}
ryeMoss
  • 4,303
  • 1
  • 15
  • 34