- Home /
Is it possible to make thousands of colliders at once?
For my game, the setting is a huge city with over 6000 buildings, and thus over 6000 meshes. It would be close to impossible for me to got into each one and add a collider, so is there a way to add a collider to the city prefab? I was thinking that a script could do it, but Googling has not turned up any results.
just go to the prefab and add a collider by pressing the add componet button but if it really necessary for the script, use the addcomponent function here the link docs.unity3d.com/420/Documentation/ScriptReference/GameObject.AddComponent.html
Answer by Loius · Jun 22, 2014 at 09:14 PM
When you add a collider to an object, Unity tries to fit the collider to the object's renderer (works with Box, Mesh, and Sphere at least). So you could just have an editor script that's something like this:
foreach(GameObject obj in objects) {
obj.AddComponent<WhateverCollider>();
}
A scriptablewizard might be your best bet:
public class BatchColliders : ScriptableWizard {
public enum ColliderType { Box, Mesh }
public ColliderType typeOfCollider;
public GameObject[] listOfObjects;
[MenuItem("Custom/Batch Colliders")]
public static void CreateWizard() {
ScriptableWizard.CreateWizard<BatchColliders>("Batch Add Colliders", "Add");
}
void OnWizardCreate() { // called when you click "Add", I think
foreach(GameObject obj in listOfObjects ) {
if ( obj.GetComponent<Collider>() ) continue;
if ( typeOfCollider == ColliderType.Box ) obj.AddComponent<BoxCollider>();
if ( typeOfCollider == ColliderType.Mesh ) obj.AddComponent<MeshCollider>();
}
}
}
http://docs.unity3d.com/ScriptReference/ScriptableWizard.html
So, something like this:
public class BatchColliders : ScriptableWizard {
public enum ColliderType { Box, $$anonymous$$esh }
public ColliderType collider;
public GameObject[] Building$$anonymous$$aterial_2439, Building$$anonymous$$aterial_2585, Building$$anonymous$$aterial_3456, etc.;
[$$anonymous$$enuItem("Custom/Batch Colliders")]
public static void CreateWizard() {
ScriptableWizard.CreateWizard<BatchColliders>("Batch Add Colliders", "Add");
}
void OnWizardCreate() { // called when you click "Add", I think
foreach(GameObject obj in listOfObjects ) {
if ( obj.GetComponent<Collider>() ) continue;
if ( typeOfCollider == ColliderType.Box ) obj.AddComponent<BoxCollider>();
if ( typeOfCollider == ColliderType.$$anonymous$$esh ) obj.AddComponent<$$anonymous$$eshCollider>();
}
}
}
I do apologize if this is wrong, I'm kind of new to JavaScript in Unity.
Your answer
![](https://koobas.hobune.stream/wayback/20220613152248im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mesh and material disappear after CreatePrefab() in C# 1 Answer
Find right mesh for collider 1 Answer
OnMouseDown() with a Mesh Collider? 2 Answers
How to get at the 'status' of a game object? 1 Answer
Collision massively misplaced 0 Answers