- Home /
How to correct add collider to every child of gameobject?
Hi all,
I've always used unity especially for 2D games. A friend, who's really interested in 3D development in unity, asked me to review a project for him. One of the questions he had was how to add colliders to "advanced" gameobjects in unity. What I'm talking about is an object that has hundreds of children. I need a way to add the appropriate mesh collider to the appropriate child. Is there a script or editor extension for this?
Answer by hexagonius · Feb 25, 2015 at 10:29 PM
foreach (Transform t in transform)
{
if (t == transform) continue; // Skip the transform itself
t.gameObject.AddComponent<MeshCollider>();
}
But I wouldn't recommend it. Instead, model one big collider in any 3D Software for the whole thing, this might get very expensive on the CPU.
Just tried this out and unfortunately this is no longer a valid solution -- in Visual Studio, I'm getting the error "'Transform' does not contain a definition for 'AddComponent' and no extension method 'AddComponent' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)"
t.gameObject.AddComponent<$$anonymous$$eshCollider>();
Your answer
Follow this Question
Related Questions
Colliders on this spider 1 Answer
Player collides with open doorway? 1 Answer
MeshCollider has different shape than MeshFilter 1 Answer
How to change meshcollider thickness 2 Answers
Making two mesh colliders collide 2 Answers