How do I make a button from a gameobject?

I am trying to make a button from a GAMEOBJECT NOT GUI.

However I only want action to start when I click specifically on the object. I have tried using “Input.OnMouseDown” but that just works when I click on the screen. I just want the object to be clicked.

I have also tried “void OnMouseDown” but the console said something was wrong.

Hint: telling us that the console saying "something went wrong" is not being specific. The console tells you exactly what went wrong, and where it occurred. But if you don't tell us, we can't help explain the problem to you.

–

2 Answers

2

So, quotes like “Please don’t give the link to a video just give me the straight answer.” are probably why your questions are being rejected. We don’t work for you; rather we try to make the community better by sharing knowledge and helping folks learn.

That being said, let me get you on the right track, then you can try it for yourself and if you have problems with your implementation, post them here with details and examples, and we’ll see what we can do to help.

When checking Input.GetMouseButtonDown, it checks that the button was pressed, but not where it was pressed.

To have it check whether it’s clicking on a game object or empty space requires a raycast to a collider, so your game object needs some kind of collider component attached. Then you need a camera reference to turn the mouse’s screen location into a world-space ray (see the documentation for the Camera class). You can then use Raycast against the collider to determine if the mouse was over the object, and only then call your button click code.

it couldn’t be simpler

there are literally 100s, perhaps thousands of QA on this:

http://forum.unity3d.com/threads/detecting-mouse-click-on-object.19450/

#if your ‘console says something is wrong’,…

take a screenshot of that and paste it here, or just copy and paste what it says. OK?

the many many QA you can read through, explain the common mistakes.