All Remove mesh Collider

Hi !!

how to in the project all remove mesh collider???

You mean to remove all mesh colliders in the scene?

MeshCollider[] colliders = FindObjectsOfType<MeshCollider>();
int len = colliders.Length;
for( int i = 0; i < len; i++ ) GameObject.Destroy( colliders[i] );

yes! remove all mesh collider but I do not know how to do

thanksā€¦

does anyone know if there is a way to do this in the editor? As in a permanent deletion of all instances of the mesh collider component from all objects in the scene?

I had to delete a bunch of mesh colliders where the mesh was null and coded the solution below.

It deletes all mesh colliders where the mesh is null on all prefabs and all game objects in the current scene. There are no warnings - it just deletes them, so if you use this please back your project up (or commit to version control) first.

If you want to delete all mesh colliders, remove lines 21 and 32 if (collider.sharedMesh != null) continue;

Thought this might help someone else - put the following in a script file called MenuItems, in an Editor folder:

using UnityEditor;
using UnityEngine;

public class MenuItems
{
   [MenuItem("Tools/Remove Empty Mesh Colliders")]
   private static void RemoveEmptyMeshColliders()
   {
      var prefabGuids = AssetDatabase.FindAssets("t:Prefab");
      var length = prefabGuids.Length;
      for (var i = 0; i < length; ++i)
      {
         var guid = prefabGuids[i];
         var path = AssetDatabase.GUIDToAssetPath(guid);
         var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);

         var colliders = prefab.GetComponentsInChildren<MeshCollider>();
         foreach (var collider in colliders)
         {
            if (collider.sharedMesh != null) continue; 
            Object.DestroyImmediate(collider, true);
            Debug.Log("Destroyed collider on prefab " + prefab.name);
         }
      }
      var gameObjects = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];
      foreach (var gameObject in gameObjects)
      {
         var colliders = gameObject.GetComponentsInChildren<MeshCollider>();
         foreach (var collider in colliders)
         {
            if (collider.sharedMesh != null) continue;
            Object.DestroyImmediate(collider, true);
            Debug.Log("Destroyed collider on gameObject " + gameObject.name);
         }
      }
   }
}

REMEMBER TO BACK UP YOUR PROJECT FIRST!

4 Likes

This solution is much appreciated! Sorry, Iā€™m a Unity noob and donā€™t do much coding so I thought I would ask if there is something similar for removing ā€˜inactiveā€™ objects as well. Some of the modular characters Iā€™ve used come in with all the clothing, hair and props embedded in the character but simply deactivated. I would love a way to remove those items rather than having to do it manually. No big deal though, Iā€™ll make do as required.

1 Like

Yes, I think I can use that as somehow Iā€™ve got duplicate meshColliders attached to everything in my scene.
butā€¦ (newbie here) how/where can I execute the above script??

Have you seen this part of Colinā€™s post:

It creates a menu item in the editor for the static method the attribute is attached to. So you run the method by clicking on ā€œToolsā€ ā†’ ā€œRemove Empty Mesh Collidersā€. But of course only when this class is in the project and compiled.

You just have to put 1 and 1 together to get 2.

1 Like