- Home /
OnMouseDown() Basic Help
I'm new to Unity and I am trying to use OnMouseDown() to register a click on a cube object. Basically, my code looks like
function OnMouseDown(){
Debug.Log("Clicked");
}
This is the entire javascript file. There are no includes, no variables, and no other functions in it.
My scene consists only of a single cube with this script attached to it. I run the scene, but nothing happens when I click on the cube in either the game view or the scene view.
The cube is in the default layer and has a box collider. I have changed nothing about the project or scene apart from adding the cube.
I have been to several other questions but none of them seem to have the answer I am looking for. At this point, the only thing I can think of is that I forgot to include something when I created the project. Any thoughts?
I just replicated this myself and it works fine. Empty scene, create cube with box collider, make sure it's in front of the camera, add script (with the same code you list above) to the cube. I click play then click on the cube and the Debug.Log prints out...
Sorry I can't see what your problem might be. $$anonymous$$aybe start with a fresh project to see what happens...
Your code and setup should work as described. Check that you have the box collider on the cube present and enabled.
Answer by Blue Blade Batoma · Nov 15, 2013 at 07:21 AM
Thanks for your answers. I figured out what's going on. I was getting an error every time I start Unity, but it didn't seem to make a difference. As it turns out, it is pausing my test of the scene after every frame, so my program hasn't actually been running.
Your answer
Follow this Question
Related Questions
Click on two objects to render third 2 Answers
How do I let the player interact with objects close to them? 1 Answer
How to left click to rotate a box in a same direction in Unity 2D? 1 Answer
Make OnMouse functions ignore GameObjects that pass in front of them 1 Answer
How to click only one once? 0 Answers