Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

How do I hide this warning on release build? BoxColliders does not support negative scale or size.

Discussion in 'Editor & General Support' started by LandonC, Jul 16, 2020.

  1. LandonC

    LandonC

    Joined:
    Dec 20, 2012
    Posts:
    83
    I have several scenes setup in previous unity versions which I have migrated to a newer unity version. Some objects are set with a negative scale and are affected by code when interacted with, so I do not plan to change that.

    The warning that remained in the release build shows me this warning

    Code (CSharp):
    1. W/Unity: BoxColliders does not support negative scale or size.
    2.     The effective box size has been forced positive and is likely to give unexpected collision geometry.
    3.     If you absolutely need to use negative scaling you can use the convex MeshCollider. Scene hierarchy path "path to asset"

    I want to remove it so it does not show on the deployed devices. Any solutions to this?


    Similar question:
    https://stackoverflow.com/questions...rning-in-unity-without-knowing-the-warning-id
     
  2. hungrybelome

    hungrybelome

    Joined:
    Dec 31, 2014
    Posts:
    336
    Bump. Need this for production as well
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,345
    If you insist on flipping or scaling your object in whacky ways and you don't like this error, just disable or remove the 2D colliders associated with those objects.

    Here's the deal: Unity takes your scene and ransacks it for all the bits that need to be transferred into the Box2D engine. All of them. That's how it works.

    For 99.99% of people, we genuinely want to know when we put a degenerate 2D collider into the game, such as one that is flipped or reversed so it has zero or negative area.

    This warning is a Good Thing(tm) for almost all of us.

    If you want to whacky-whacky flip your stuff around and scale it negative and zero sizes, then it's on you to either fix the problems or tolerate the warnings.

    It's trivial to split up your prefabs into rotating and non-rotating parts, such as this:

    PlayerPrefab <--- all Collider2D and Rigidbody2D go here
    VisualPortionsThatMayGetFlippedOrRotatedOrScaledOrNegated


    Your root object never flips around, just the visual portion.
     
  4. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    294
    Bug happens in 3D also. It's a bug because collision works perfectly and pretty much every art lib mirrors stuff by negative scaling. Also wasn't an issue in previous Unity versions. No need to spam my console window.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,345
    Everything I said still applies in 3D.

    Software changes. What used to be possible no longer is, and new things are possible.

    Console spam is to let you know: What you are doing will not work.

    Adapt. Overcome. It's a trivial change. Make it. Make your game. Have fun.
     
    Last edited: Oct 31, 2023
  6. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    294
    Ok, setting pure technical prowess against marketing blah. I wrote an EditorWindow which should correct BoxColliders, thus bypassing the bug.
    Copy to any Folder named "Editor". Open Window under MainMenu->"Tools/SnapTool3/Other Tools/Correct Box Colliders...". Change the path to your liking in file.

    Rules:
    If there is no MeshRenderer on the BoxCollider, the negative axis is just flipped, effectively mirroring the BoxCollider.

    If there is a MeshRenderer on the BoxCollider and it has an offset to the Transform, a new GameObj is created as child holding a new BoxCollider. The old BoxCollider is removed.
    If there is no offset, the negative axis is just flipped.

    Worked for me. No guaranties. Good luck.
     

    Attached Files:

    halley likes this.
  7. Miscellaneous

    Miscellaneous

    Joined:
    Sep 24, 2013
    Posts:
    54
    Curious, if collision and flipping is required (for modular level building) what non whacky way can you suggest?
     
  8. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,551
    If for some reason you can't just fix the box yourself, you can throw this editor command script at the object. Explanation in the comments.

    Code (CSharp):
    1. public class FixNegativeBoxColliders: MonoBehaviour
    2. {
    3.     //
    4.     // If the effective scale of a BoxCollider ends up making the .size negative,
    5.     // such as when a prefab is scaled (-1,+1,+1), then Unity complains loudly:
    6.     //
    7.     //   /!\  BoxColliders does not support negative scale or size.
    8.     //
    9.     // This works around the problem by looking at the signs of the lossy scale,
    10.     // and flipping the same signs in the box collider's size vector.  Since
    11.     // the box collider is always aligned with the local matrix, this has no
    12.     // effect on the collider except to quiet the console warnings.
    13.     //
    14.     // Select the offending object with a BoxCollider, or the root object(s)
    15.     // which might contain BoxColliders, and run this command.  It only makes
    16.     // an actual change when the current settings need to be changed.
    17.     //
    18.     [MenuItem("Edit/Tools/Fix Negative Box Colliders")]
    19.     public static void FixSelectedNegativeBoxColliders()
    20.     {
    21.         if (Selection.activeGameObject == null)
    22.             return;
    23.  
    24.         // editing a Prefab in isolation (or maybe other weird situations)?
    25.         if (StageUtility.GetStage(Selection.activeGameObject) !=
    26.             StageUtility.GetMainStage())
    27.                 return;
    28.  
    29.         Undo.IncrementCurrentGroup();
    30.         Undo.SetCurrentGroupName("Fix Negative Box Colliders");
    31.         int undoID = Undo.GetCurrentGroup();
    32.  
    33.         foreach (GameObject target in Selection.gameObjects)
    34.         {
    35.             foreach (BoxCollider box in
    36.                 target.GetComponentsInChildren<BoxCollider>())
    37.             {
    38.                 FixNegativeBoxCollider(box);
    39.             }
    40.         }
    41.         Undo.CollapseUndoOperations(undoID);
    42.     }
    43.  
    44.     public static void FixNegativeBoxCollider(BoxCollider box)
    45.     {
    46.         Vector3 lossy = box.transform.lossyScale;
    47.         Vector3 flip = new Vector3(
    48.             Mathf.Sign(lossy.x),
    49.             Mathf.Sign(lossy.y),
    50.             Mathf.Sign(lossy.z));
    51.         Vector3 sign = new Vector3(
    52.             Mathf.Sign(box.size.x),
    53.             Mathf.Sign(box.size.y),
    54.             Mathf.Sign(box.size.z));
    55.         if (flip != sign)
    56.         {
    57.             Undo.RecordObject(box, "Fix Negative Box Collider");
    58.             box.size = Vector3.Scale(box.size, flip);
    59.         }
    60.     }
    61. }
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,345
    Put the colliders somewhere else that doesn't flip.

     
  10. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,551
    For me, this happened because a library of prefabs included a "left end of curving rail" but not a "right end of curving rail" and just flipped the scale to make it work visually. Which, in my opinion, is a fine thing for you to expect level artists to do.

    Honestly, with the simple code I provided above, it's kind of a nuisance that BoxCollider can't just fix itself internally. MeshCollider works, CapsuleCollider works, SphereCollider works, ...
     
    Kurt-Dekker likes this.
  11. jo82

    jo82

    Joined:
    Dec 3, 2015
    Posts:
    47
    I tried your script but it does nothing in my case. I still get the warnings. I'm on 2022.3.16f1