x


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?

more ▼

asked Oct 20 '11 at 07:29 PM

avidesk gravatar image

avidesk
53 5 8 9

Sorry to ask, but is the parent object enabled?

Oct 20 '11 at 09:10 PM jahroy

of course :)

Oct 20 '11 at 10:50 PM avidesk

paste the code and ill take a look

Oct 21 '11 at 01:45 PM CarlLawl

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 OnMouseDown () { _active = !_active; }

(Don't know why my code is formatted like that, the line breaks are normal in the textbox I'm typing in)

Oct 21 '11 at 08:38 PM avidesk

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 naming issue? Can't help but asking after seeing a variable named _active....

Oct 21 '11 at 09:08 PM jahroy
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Oct 21 '11 at 09:17 PM

jahroy gravatar image

jahroy
3.2k 14 18 42

I don't think this is the case. I am only using two routines in the script. One is OnMouseDown, 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!

Oct 21 '11 at 09:28 PM avidesk
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1002
x699
x232
x78

asked: Oct 20 '11 at 07:29 PM

Seen: 958 times

Last Updated: Oct 21 '11 at 09:30 PM