Function OnMouseDown()

Hello!

I am trying to use the function OnMouseDown(), but for somereason it doesnt work…

I have already tried the script in this page…

and this one also doesn’t work…

Does any one knows what is the problem?

Best Regards,
topastop

P.S. The script is attached to my main camera.

The script should be on whatever object is receiving the click. That object also needs some kind of collider on it.

Greetings,

Post your code, pluss what you want to happen in your game, and I am sure we can figure it out! :slight_smile:

That would be why it doesn’t work.

OnMouseDown is supposed to be used in scripts attached to objects in the scene. These objects must have a collider, and when the mouse is pressed while the cursor is over them in scene, this method gets called.

I think what he is actualy looking for is to check if the player is pressing the mouse key down, and not down ON anything!

I have seen manny do this mistake before, and thought that, that event is triggering on Mouse Down regardless of where, and when…

So, if you can post what you want to happen it can be abit easier for us to help you out! :slight_smile:

and if I am right about what you are trying to do, this is the code your looking for to use;

if(Input.GetKeyDown(KeyCode.Mouse0)) {
// do something
}

Use this in Update function!

If that’s the case then a clearer way would be to use Input.GetMouseButtonDown(0)

Thank you very much!

That was precisly what I was looking for :slight_smile:

I didn’t knew that the function OnMouseDown, shall have been attached to game objects.

Thanks a lot everybody :slight_smile:

topastop

True, I just forgot while I didnt have the API infront off me! :slight_smile: hehe!