Is OnMouseDown effcient to use?

Hi,

I’ve researched several ways to ‘click’ a game object.

OnMouseDown seems to be the easiest to use, so I question its efficiency. How efficient is it in general?

Would it be more efficient if I was to use raycasters that ray-casts from the camera to the game object and create some sort of custom event system? (my understanding on this is lacking atm)

Would using ScreenToWorldPoint or ScreenPointToRay use less memory etc?

If anyone can point me in the direction of ‘how OnMouseDown and any other function’ was implemented, that’ll be gold, I’ll race you.

If I’m not mistaken, I think OnMouseDown (and other mouse methods) uses raycasting under the hood, since you need to have a BoxCollider to make it work. Once your mouse event reacts to game objects box collider, Unity then uses SendMessage(“OnMouseDown”, SendMessage.DontRequireReceiver).

I would personally use OnMouseDown and other built in functions. When you’re close to releasing your game and performance monitoring shows you’re wasting cpu power / allocating lots of memory, I would refactor the code and implement a self made solution.

1 Like

According to the API, OnMouseDown works with any collider or GUI elements, not just boxes.

Thanks guys :smile: