How can I get mouse clicks on an object that scales up from zero?

OnMouseUp will detect a click on an object's collider. Setting transform.localScale to zero disables the collider. No great surprise, the collider gets scaled to nothing, so it can't collide with anything. What is surprising is that when I set the localScale back to non-zero, the collider still doesn't work.

This is a problem, because I want an object to grow from nothing, and be clickable the whole time.

To demonstrate, use GameObject > Create Other > Plane in the editor, then attach this (C#) component to it. The scale is set to zero in Start(), then immediately back to one -- but now the OnMouseUp never gets hit. If I change the Vector3.zero to `new Vector3(0.00001f, 0.00001f, 0.00001f)`, then it works fine. This is my workaround, but this seems like a bug, or am I missing something?

using UnityEngine;

public class ClickableQuestion : MonoBehaviour
{
    void Start()
    {
        transform.localScale = Vector3.zero;
        transform.localScale = Vector3.one;
    }

    void OnMouseUp()
    {
        Debug.Log("clicked " + name);
    }
}

Thanks for any tips.

EDIT 1: I've discovered that the collider gets disabled if scale is 0 while the game object is active. The following lines of code do not disable the collider ...

gameObject.active = false;
transform.localScale = Vector3.zero;
transform.localScale = Vector3.one;
gameObject.active = true;

But these lines do disable it ...

gameObject.active = false;
transform.localScale = Vector3.zero;
gameObject.active = true;
transform.localScale = Vector3.one;

Not very helpful for my purposes, but perhaps interesting.

EDIT 2: Another thing I just noticed -- setting scale very close to 0 seems to be a bad/slow thing to do. I have a component that does this in OnEnable:

transform.localScale = new Vector3(Mathf.Epsilon, Mathf.Epsilon, Mathf.Epsilon);

There is a very noticeable 2 or 3 second lag between when I activate the object with this component and when it starts updating. If instead of Mathf.Epsilon I use 0.0001 then there is no noticeable lag.

Just thinking out loud: Couldn't you take the collider component size into a variable and restore it after resizing the GameObject (I mean, independantly) ?

The collider doesn't have a size independent of the game object, so no, not really.

2 Answers

2

create another object and attach the collider to that object. then make your main (scaling) object the child of the collider object. change the size of your collider based on the scale of the scaling object but never set it to 0 and in OnMouseUp check if the scale of the scaling object is 0 then don't execute your mouse event code.

it's a bug so submit it to unity technologies.

Simpler just to avoid setting to zero in the first place, which is what I'm now doing. I submitted a bug through the Unity Editor Help menu.

I'm guessing this could be because you're setting it to zero, at which point any 'scaling' (which is basically multiplying) would mean multiplying something times zero. Have you tried simply setting it to 0.001 instead?

Yes, as I said above, I tried 0.00001 and it worked. My question is why setting to zero cripples the collider forever, even if after setting it to zero I set it to something non-zero?

That sounds familiar. I think I ran into that a long time ago. The work around was to never let it hit zero. I think it's a bug.

As I said, I don't think it's a bug. Relative scale is scale relative to the parent. If there is no parent it's scale relative to it's import settings. Once it hits zero you're multiplying with zero..

Sorry Joshua, you're still not following me -- even when I set the scale back to 1 the collider still doesn't work. If the scale is ever zero then the collider never works again.