# How can I detect a Gameobject touching another gameObject without using collider2D's?

I have fish gameObjects that have colliders2D so that I can click and move them. I added fish food with Collider2D's but it was hard for me to click on the fish. After I disabled the fish food collider2D it was easier to click on the fish. Is there an alternative when detecting two gameObjeccts without colliders?

• Why don't you simply put the fish food on a different layer, and ignore that layer in your fish-click detection script? Getting rid of colliders seems like the wrong solution to this problem. – DMGregory Nov 13 '20 at 21:49
• I tried Physics2D.IgnoreCollision(algae.GetComponent<CircleCollider2D>(), GetComponent<BoxCollider2D>()); but didn't work and i changed the Physics2D from project settings to ignore the fish food – MM1Content Nov 13 '20 at 21:53
• I'm also using OnMouseDown() for click – MM1Content Nov 13 '20 at 21:55
• Well there's your mistake. – DMGregory Nov 13 '20 at 21:57
• lol OnMouseDown? – MM1Content Nov 13 '20 at 22:02

Don't use OnMouseDown. Having this in your project forces Unity to cast rays for you in the most generic way, which is often less efficient and less precise than casting them yourself.

Make your own script that can filter to the physics layer you care about.

public class FishClicker : MonoBehaviour {

void Update() {

if (Input.GetMouseButtonDown(0)) {
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var hit = Physics2D.OverlapCircle(mousePos, clickRadius, whatIsFish);

if (hit != null) {
var fish = hit.GetComponentInParent<Fish>();

if (fish != null) { fish.DoClickThing(); }
}
}
}
}

• Thank you so much! Cheers :) – MM1Content Nov 13 '20 at 22:09
• Is there a way to draw the Physic2D.Overlap? So that I can see the radius to fine tune it. – MM1Content Nov 13 '20 at 22:27
• void OnDrawGizmos() { Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Gizmos.DrawWireSphere(mousePos, clickRadius); } – DMGregory Nov 13 '20 at 22:54
• Awesome Appreciate it – MM1Content Nov 13 '20 at 23:19
• At the moment I have seven different colored kind of fish. For instance every time a blue fish is instantiated it has the blue fish script. Your code is moving multiple blue fish at the same time lol so does that mean each colored fish prefab needs its own script? – MM1Content Nov 13 '20 at 23:28