Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

All Remove mesh Collider

Discussion in 'Scripting' started by udede, Jun 29, 2014.

  1. udede

    udede

    Joined:
    Jul 26, 2011
    Posts:
    72
    Hi !!

    how to in the project all remove mesh collider???
     
  2. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    You mean to remove all mesh colliders in the scene?

    Code (csharp):
    1. MeshCollider[] colliders = FindObjectsOfType<MeshCollider>();
    2. int len = colliders.Length;
    3. for( int i = 0; i < len; i++ ) GameObject.Destroy( colliders[i] );
     
  3. udede

    udede

    Joined:
    Jul 26, 2011
    Posts:
    72
    yes! remove all mesh collider but I do not know how to do

    thanks..
     
  4. wadams-hookbang

    wadams-hookbang

    Joined:
    Dec 11, 2017
    Posts:
    2
    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?
     
  5. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    242
    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:

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. public class MenuItems
    6. {
    7.    [MenuItem("Tools/Remove Empty Mesh Colliders")]
    8.    private static void RemoveEmptyMeshColliders()
    9.    {
    10.       var prefabGuids = AssetDatabase.FindAssets("t:Prefab");
    11.       var length = prefabGuids.Length;
    12.       for (var i = 0; i < length; ++i)
    13.       {
    14.          var guid = prefabGuids[i];
    15.          var path = AssetDatabase.GUIDToAssetPath(guid);
    16.          var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
    17.  
    18.          var colliders = prefab.GetComponentsInChildren<MeshCollider>();
    19.          foreach (var collider in colliders)
    20.          {
    21.             if (collider.sharedMesh != null) continue;
    22.             Object.DestroyImmediate(collider, true);
    23.             Debug.Log("Destroyed collider on prefab " + prefab.name);
    24.          }
    25.       }
    26.       var gameObjects = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];
    27.       foreach (var gameObject in gameObjects)
    28.       {
    29.          var colliders = gameObject.GetComponentsInChildren<MeshCollider>();
    30.          foreach (var collider in colliders)
    31.          {
    32.             if (collider.sharedMesh != null) continue;
    33.             Object.DestroyImmediate(collider, true);
    34.             Debug.Log("Destroyed collider on gameObject " + gameObject.name);
    35.          }
    36.       }
    37.    }
    38. }
    39.  
    REMEMBER TO BACK UP YOUR PROJECT FIRST!
     
  6. cybermynd

    cybermynd

    Joined:
    May 22, 2018
    Posts:
    1
    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.
     
    Colin_MacLeod likes this.
  7. bhoffman67

    bhoffman67

    Joined:
    May 14, 2022
    Posts:
    18
    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??
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,543
    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.
     
    Colin_MacLeod likes this.