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) ?
– by0log1cThe collider doesn't have a size independent of the game object, so no, not really.
– yoyo