I am trying to get object picking (using raycasting) going and after spending hours on the forum and reading the docs, I have no choice but posting a new topic for something that’s probably been answered before. Apologies if it’s the case…
I have a little raycasting script that works fine if attached to a GameObject.
But I want to detect when any object is clicked in the scene, not just the objects I have attached this script to.
Basically I want to know how to setup the equivalent of “if mouse down on anything do …”
I thought I could basically attach the script to any GameObject (even an empty one), but in that case the OnMouseDown function of the script isn’t triggered.
It should be a very basic thing but I can’t see it explained anywhere in the docs…
OnMouseDown works for object picking, but the object must have a collider, otherwise its just “nice to look but not to interact”.
exception naturally is if you are on the iphone, as onmousedown does not exist there for performance reasons (the mouse is always down on the touch)
On the iphone you would have to use update, iPhoneInput and look if a touch just began and then use Physics.Raycast there
I think what I didn’t get is that all scripts have to be attached to an object and that there is no Scene object that we can use as the root-level object for event management.
I got what I wanted working now by having a “selectable” script attached to all objects that can be selected. That script uses OnMouseDown and seems to work fine without the need for any raytracing, so it might actually end up being more performant…
I find it surprising that mouse picking isn’t even mentioned in the manual (from what IU can see…)