Collider on object with localScale(0,0,0) - OnMouseDown()

I am using localScale = new Vector3(0,0,0) as a means of hiding game objects rather than SetActive(false).

In this case I want to toggle the game object as visible and invisible on mouse clicks.

OnMouseDown() requires a collider which I have.

When I click on the object and it becomes invisible, the collidor for that object in the editor remains unchanged

However OnMouseDown() is not invoked on game object of localScale 0,0,0

Is there anyway of getting OnMouseDown() to work regardless of the gameObject’s localScale?

Or perhaps some other method of detecting a mouse click on the gameObject?

don’t set it’s local scale to 0. just disable/enable the renderer component if you want to hide it while keeping it active

with a scale of 0 you can’t raycast to check if your mouse is over it, cause its not taking up any space

What happens if you have an empty container object with a script on it but no mesh and it contains other game objects with meshes and you just want to show and hide the container object.

Can’t use SetActive because then it wont respond to any events, you can’t use your method because it does not have a mesh and I can’t use localScale(0,0,0) because then it wont respond to mouse events.

I ended up solving it with an overrideable DoShow(bool bShow) function but this is a this is a bit of bloody pain with Unity - there needs to be one consistent way of showing and hiding objects while leaving them responsive!

if its a container object then you can use GetComponentsInChildren() and loop through the array it returns and toggle the scripts enable property. this is by far the least intrusive way to toggle gameobject’s visiblity and keep them enabled.

Monobehaviours actually do respond to events just fine even when disabled. OnCollision and OnTrigger calls do infact still be called. and any event (Event, UnityEvent, Delegate, or otherwise) you subscribe to can still trigger a disabled object just fine.

It just doesn’t call the Awake/Start/Update/FixedUpdate, which is intended, Theres a difference.

Surely that would just be adding unnecessary CPU overhead.

It would be ideal if an empty gameobject had a show/hide flag (that did not disable Start, Update and Awake) and the unity engine just halted rendering all child objects before it even started.

you’re tackling a problem in the wrong order.

make it work, then make it right, then make it fast.

you’d find that working in this order you’ll discover that you usually won’t have to worry about performance until it matters.
actually try it first, when performance does fail, its very likely not because of this.

calling Find() in update… thats a performance issue.
spawning hundreds of projectiles per second… thats a performance issue
decompressing images for a 360 VR in realtime… thats a performance issue…

but calling GetComponentsInChildren on mouse click… not even a drop in the bucket.

OK, well even if it is not a noticeable performance issue it would still be ‘nice’ to have the a fore mentioned feature even if it just saves you some additional typing for a longer function.

If you want to keep everything as is except if it is visible or not, just toggle the Renderer.

Yeah but the problem was that my empty container game object did not have a renderer - it has a script that does some stuff but no visible component. I just want to show and hide the container and not have to iterate through all the children.

I want the container object to act as the visibility controller for all the children regardless of whether or not it has a renderer.

What this means at present is that I cannot have a single standard Show/Hide function in my base class - I have to override it and change the implementation depending on whether the game object to which the script is attached has a renderer on it also or whether or not it should accept mouse input while invisible.

It is solvable but it is an annoyance!