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

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

W/Unity: BoxColliders does not support negative scale or size.
    The effective box size has been forced positive and is likely to give unexpected collision geometry.
    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:

Bump. Need this for production as well

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™ 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:

VisualPortionsThatMayGetFlippedOrRotatedOrScaledOrNegated```

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

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.

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.

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.

9442616–1324832–CorrectBoxColliderWindow.cs (6.61 KB)

2 Likes

Curious, if collision and flipping is required (for modular level building) what non whacky way can you suggest?

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.

public class FixNegativeBoxColliders: MonoBehaviour
{
    //
    // If the effective scale of a BoxCollider ends up making the .size negative,
    // such as when a prefab is scaled (-1,+1,+1), then Unity complains loudly:
    //
    //   /!\  BoxColliders does not support negative scale or size.
    //
    // This works around the problem by looking at the signs of the lossy scale,
    // and flipping the same signs in the box collider's size vector.  Since
    // the box collider is always aligned with the local matrix, this has no
    // effect on the collider except to quiet the console warnings.
    //
    // Select the offending object with a BoxCollider, or the root object(s)
    // which might contain BoxColliders, and run this command.  It only makes
    // an actual change when the current settings need to be changed.
    //
    [MenuItem("Edit/Tools/Fix Negative Box Colliders")]
    public static void FixSelectedNegativeBoxColliders()
    {
        if (Selection.activeGameObject == null)
            return;

        // editing a Prefab in isolation (or maybe other weird situations)?
        if (StageUtility.GetStage(Selection.activeGameObject) !=
            StageUtility.GetMainStage())
                return;

        Undo.IncrementCurrentGroup();
        Undo.SetCurrentGroupName("Fix Negative Box Colliders");
        int undoID = Undo.GetCurrentGroup();

        foreach (GameObject target in Selection.gameObjects)
        {
            foreach (BoxCollider box in
                target.GetComponentsInChildren<BoxCollider>())
            {
                FixNegativeBoxCollider(box);
            }
        }
        Undo.CollapseUndoOperations(undoID);
    }

    public static void FixNegativeBoxCollider(BoxCollider box)
    {
        Vector3 lossy = box.transform.lossyScale;
        Vector3 flip = new Vector3(
            Mathf.Sign(lossy.x),
            Mathf.Sign(lossy.y),
            Mathf.Sign(lossy.z));
        Vector3 sign = new Vector3(
            Mathf.Sign(box.size.x),
            Mathf.Sign(box.size.y),
            Mathf.Sign(box.size.z));
        if (flip != sign)
        {
            Undo.RecordObject(box, "Fix Negative Box Collider");
            box.size = Vector3.Scale(box.size, flip);
        }
    }
}

Put the colliders somewhere else that doesn’t flip.

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, …

1 Like

I tried your script but it does nothing in my case. I still get the warnings. I’m on 2022.3.16f1