How to make sure only one thing can be clicked at a time.

I have having a little trouble with my simple game sadly. I am trying to figure out how to make it so the user can only click one ball at a time. When the balls intersect each other and they are both inside each other and the player clicks the right spot he clicks them both which I don’t want. I was thinking you can use RayCast2D (because this is a 2D game) but it isn’t working.

Here is some code

	void Update () {

        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(transform.position), Camera.main.ScreenToWorldPoint(Input.mousePosition));

        if(hit != null && hit.collider != null){

            if (Input.GetMouseButtonDown(0))



This isn’t working like I wanted it to and I am new to this so it was kind of expected.

I assume the balls use 2D physics as well? That is, Rigidbody2Ds and Collider2Ds? In that case, you can simply use Physics2D.OverlapPoint to determine what was clicked. It will return only one object, the one with the lowest z coordinate, but you can use layers (or Physics2D.OverlapPointAll) if you want to decide for yourself which object should register the click.

The code should look something like this:

void Update() {
    if (Input.GetMouseButtonDown(0)) {
        Collider2D clickedCollider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));
 		Debug.Log("Screen point: " + Input.mousePosition);
 		Debug.Log("World point: " + Camera.main.ScreenToWorldPoint(Input.mousePosition));

        if (clickedCollider != null) {
        	Debug.Log( + " clicked!");
        else {
        	Debug.Log("Nothing clicked!");

NOTE: You can remove all the Debug.Log lines once it is working the way you expect it to, those are there just to help you figure out what is going on!

Also note that unlike in your code, you don’t have to use GameObject.Find or the gameobject name when destroying the object, you can get the gameobject reference directly from the collider. This applies to using RaycastHits as well.