Using Prefabs as buttons

Hello everyone,

New poster and fairly experienced Unity user here!
I’ve run into a small problem with one of my projects, and after furiously searching google I have decided to come here for answers.

The problem:
In my project, our lead-designer decided to instead of using the OnGUI() function for the menu, he wanted 3D objects as buttons.
The problem is, I don’t know how to get this working.
I’ve tried several RayCast tutorials, tried attaching scripts to these prefabs to see if the name of the prefab pops up through simple onMouseDown() functions (Note, this does work on other non-prefab gameObjects)
One rayCast script worked, but it printed all the button names, and not the one I clicked on which is likely due to the fact that this script was attached to all buttons on-screen.

If anyone has any advise on how to get this working through each individual button having the same script, or otherwise enabling it through my Main Script which initializes the Menu (Attached to camera), that’d be great.

Hope I was clear enough, and if not, I’ll provide more information if necessary.

-Zubaja-

make them have triggers and the on mouse down and on mouse over functions will work. if you put colliders on it that aren’t set to trigger you need to put a rigidbody on it or else it won’t register.

Thanks for the reply.

I managed to find another workaround; Placing the prefabs in other GameObjects works aswel.
Count this problem as solved!

-Zubaja-

That specific problem gets asked soooo often.

You have to test raycasts against ‘this’ object. You simply cannot use object names in raycast tests - not only is it slower, but as soon as you have two objects with the same name, they’ll both think you’re clicking on them.

The OnMouseDown, OnMouseDownAsButton, and OnMouseOver functions are your friends. (with the collider requirements as said above)