- Home /
Mouse Click not detected
I have a script that has an OnMouseDown function, and toggles a boolean value. The object I want the script attached to is a child object, 2 or 3 levels deep in my main parent game object. If I attach the script to that main parent object or any of its children, it doesn't do anything. If I attach the script to any other object in my scene, it works as expected. All of the objects I try to use have a box collider component.
Is there any reason why this would be happening?
The code isn't the problem unfortunately. It's as simple as it gets, and works on any object I test it on, except my main object (a vehicle), or any of its child components.
private bool _active; void On$$anonymous$$ouseDown () { _active = !_active; }
(Don't know why my code is formatted like that, the line breaks are normal in the textbox I'm typing in)
You have to type it in an answer box, then format, then paste into a comment box to get it formatted right.
Wish I could help you with your question!
Is it due to a variable na$$anonymous$$g issue? Can't help but asking after seeing a variable named _active....
Answer by jahroy · Oct 21, 2011 at 09:17 PM
This might be stupid, but is there a chance that OnMouseDown could be getting called multiple times or getting called from OnGUI by the object in question?
I've had trouble in the past using someBoolean = ! someBoolean when certain functions return true multiple times.
For example, I first tried to use this when impementing keyboard shortcuts with Input.GetKey and determined that I had to use Input.GetKeyDown. Using GetKey would return true multiple times, which would rapidly toggle the value with unpredictable results.
That being said, you're using OnMouseDown() which is part of MonoBehaviour... And it doesn't sound like this matches your issue... So all this typing is probably a waste of time.
I guess this would only apply if you're explicitly calling OnMouseDown from another script.
I don't think this is the case. I am only using two routines in the script. One is On$$anonymous$$ouseDown, and the other is OnGUI, which simply checks the state of '_active', and displays a GUIButton if it is true. There is very little code involved, I doesn't seem likely that there are any variables or functions interfering with one another.
Thanks for the suggestion though.
Given the fact that it works perfectly with any object in the scene except for the one I want (lame), it almost has to be an issue with the object I am attaching the script to, and not the script itself. I just have no idea what could be causing this.
Because this is such an odd issue, I'm sure I sound like a loon, but I promise I have been using Unity for several months with great success until this little hangup!