Search Unity

Bug HideFlags.DontSaveInEditor - Wrong collider Bounds (center always 0)

Discussion in 'Scripting' started by Darkgaze, May 16, 2022.

  1. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    I have an object that I instantiate.

    Then I set the hide flags to HideFlags.DontSaveInEditor.

    I gather the combined bounds of all children colliders using:

    Code (CSharp):
    1.  
    2. public static Bounds GetBoundsFromObject(GameObject go)
    3.         {
    4.             var colliders = go.GetComponentsInChildren<Collider>();
    5.  
    6.             if (colliders.Length == 0)
    7.                 return new Bounds(go.transform.position, Vector3.zero);
    8.  
    9.             Bounds fullBounds = colliders[0].bounds;
    10.             for (int i = 1; i < colliders.Length; i++)
    11.             {
    12.                 fullBounds.Encapsulate(colliders[i].bounds.min);
    13.                 fullBounds.Encapsulate(colliders[i].bounds.max);
    14.             }
    15.             return fullBounds;
    16. }

    This returns 0 always when that HideFlag is set.
    Debug.Log(colliders[0].bounds.center);

    Otherwise, works perfectly.

    Is there any reason why or is this a bug?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    That option AFAIK removes it from the scene so it's not part of anything. It cannot have bounds if it's not active and part of a scene.

    You can see that mentioned in the HideFlags docs:
    https://docs.unity3d.com/ScriptReference/HideFlags.html

    This would be the same as a prefab (not an instance).
     
  3. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    Well. I created another script and seems like the flags are not affecting the Bounds. Bounds are ok.
    What is affected is calling "transform.Translate" and such , because there is no world space for this object! It doesn't move and stays in 0, so the bounds are also in 0.

    So yes. This flag affects the transform, but not the bounds. :) Thanks!
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    I don't follow to be honest. You're saying the bounds are okay but if you change the Transform then they're not? That would be very odd but maybe the bounds are initially guessed in 3D physics then the change of Transform resets them, not sure.

    For 2D physics this wouldn't work at all because the bounds are calculated from the actual physics shapes created and there wouldn't be any if it were not in a scene.

    EDIT:
    I took a quick look at 3D physics and sure enough, there won't be any bounds if there are not any shapes so I'm not following why you'd ever see any bounds. The bounds you do get would be centered on the transform position but a degenerate size still.
    Code (CSharp):
    1. AABB Collider::GetBounds()
    2. {
    3.     if (m_Shape)
    4.     {
    5.         GetPhysicsManager().AutoSyncTransforms();
    6.  
    7.         return getShapeWorldBounds(*m_Shape);
    8.     }
    9.     else
    10.     {
    11.         return AABB(GetComponent<Transform>().GetPosition(), Vector3f::zero);
    12.     }
    13. }
     
  5. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    Thanks a lot.
    I was just saying that calling Transform on an object that has those hide flags enabled doesn't work, and that it makes sense because there is no space for those objects. The object stays in 0 and the bounds around that object in the origin of the world space.