- Home /
OnMouseDown() not detecting click every frame
My script, as you can see, Destroys gameObject on the first click on it. If there' s more gameObjects and i click on them faster, some of them need 1 click to destroy (that is what i want) and some of them need several clicks to destroy (is it because of the detection of my click is not called every frame? Or i don't really know) Script is atrached to gameObject.
OnMouseDown()
{
Destroy(gameObject);
}
Answer by Harinezumi · Feb 06, 2018 at 10:21 AM
A quick search resulted in the following similar issues:
https://answers.unity.com/questions/486847/onmousedown-doesnt-work-1.html (check the second highest voted answer as well!)
https://answers.unity.com/questions/425478/onmousedown-not-firing.html
So it seems the problem can be caused by various reasons:
- incorrect setup (e.g. no collider on the object)
- camera view issues (too close to the camera or not seen by the main camera)
- another collider is blocking the view
Apart from these, how fast does your application run? At 60 FPS it is not very likely that you are clicking faster than a frame, even if I assume that OnMouseDown() is synced to the physics update. Which means the problem has an issue in the logic.
@Inblinder thank you for accepting the answer. For the sake of others who run into a similar situation, could you tell what exactly caused the problem?
I havent really corrected it. It is same as it was.. i cant find the answer to this... i accepted it for your trying to help. $$anonymous$$by its because of my pc.
Well thank you, but that's not very useful for you, is it? ;)
So I just tried the above script in a project, and it works just as expected, destroying one object per click (and my FPS wasn't even at 60). But I just remembered that I had a similar issue, and it was caused by my mouse beco$$anonymous$$g unresponsive, caused by a broken cable.
Could the mouse be an issue?
I hope it is not of my mouse cabel... because some guy just realized that the raycast must be Raycast2d... i havent tryied his script yet but i hope it will fix all probs. So i remade it by using raycasts in Update(). (So it will detect my click every frame). This guy helped my with the raycast type. So after i will try it i'll write you back and send you the script. Anyway thanks for help.
I have read somewhere that the raycasts that you recomended to me work only for 3d objects. :(
Your answer
Follow this Question
Related Questions
OnMouseDown() function 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Ways to make OnMouseDown (or a single script) differentiate between different colliders/sprites 1 Answer
Only Allow One Click on GameObject? 2 Answers