In order for an object to be detectable by the collision detection system of the engine, it needs a collider component. There are both 2d and 3d colliders that come in different shapes. So you need to pick the one that's most appropriate for the shape of your object and then adjust its dimensions accordingly.
After you added a collider to an object, there are different ways to detect collisions with it:
If it's about detecting mouse cursor clicks, then having a script that implements the
IPointerClickHandlerinterface isn't a bad approach. No, it's not obsolete. It was just moved from the engine core to a package, just like the rest of the UI system. You now find it in the documentation forcom.unity.ugui.In order for it to work, it requires two additional things to be present in your scene:
- a Physics RaycasterPhysics Raycaster and/or Physics 2D RaycasterPhysics 2D Raycaster component on your camera (depending on whether you want to detect 3d or 2d colliders).
- An Event System component somewhere in the scene.
If it's not about mouse cursor clicks (or at least not directly), you can use the
Physicsclass (for 3d colliders) orPhysics2Dclass. They offer several methods to detect all colliders currently overlapping various geometrical shapes.You can mark colliders as triggers. When you do that, then scripts on these objects will receive OnTriggerEnter, OnTriggerExit and OnTriggerStay messages. Note that these will only trigger for objects that have Rigidbody components in addition to a collider.
When both colliders have rigidbodies (or when one of them is static), which means that the engine treats them as solid objects, then they will also trigger OnCollisionEnter, OnCollisionExit and OnCollisionStay messages as soon as they get into contact. These messages have the advantage over OnTrigger messages that they contain a lot more information on how exactly the collision occured.
More information in the manual.