Daniel Lip
Daniel Lip

Reputation: 11319

How can i get all the Mesh Colliders Components i added to all models and objects in Hierarchy?

To add them all i marked and selected in the Hierarchy all the models and objects and then in the meny i did Component > Physics > Mesh Collider

But now i want to delete/remove all the Mesh Colliders i added. But i want to do it with a script.

What i want is to list in array or List all the Mesh Colliders and then to decide what to do with each one of them if to delete rename or anything else.

GameObject.FindObjectsOfType(typeof(MonoBehaviour)); //returns Object[]
GameObject.FindGameObjectsWithTag("Untagged");  //returns GameObject[]

This will return all the gameobjects but it's not what i need.

Upvotes: 1

Views: 1458

Answers (1)

Programmer
Programmer

Reputation: 125275

With FindObjectsOfType:

To find with MeshColliders with FindObjectsOfType, provide MeshCollider as the type. Convert it into MeshCollider array.

    MeshCollider[] allMeshes = GameObject.FindObjectsOfType(typeof(MeshCollider)) as MeshCollider[];
    for (int i = 0; i < allMeshes.Length; i++)
    {
        allMeshes[i].enabled = false; //Optional
        Destroy(allMeshes[i]);
    }

With FindGameObjectsWithTag:

Find the GameObject that contains the Colliders with their tags. Store to GameObject array then use getComponent to get the MeshCollider from them.

    GameObject[] tempObj = GameObject.FindGameObjectsWithTag("Untagged") as GameObject[];
    MeshCollider[] allMeshes = new MeshCollider[tempObj.Length];


    for (int i = 0; i < allMeshes.Length; i++)
    {
        allMeshes[i] = tempObj[i].GetComponent<MeshCollider>();
        if (allMeshes[i] != null)
        {
            allMeshes[i].enabled = false; //Optional
            Destroy(allMeshes[i]);
        }
    }

Upvotes: 1

Related Questions