- Home /
How to assign procedural mesh to a collider
How do I assign a procedural mesh to a collider? The mesh name that I assign in the script isn't listed in the Mesh drop down until I run the game. At that point I can pause the game, assign the mesh and everything works fine but I can't assign before running.
The code that I'm using now:
gameObject.AddComponent(MeshFilter); gameObject.AddComponent("MeshRenderer"); gameObject.AddComponent(MeshCollider);
var mesh : Mesh = GetComponent(MeshFilter).mesh; mesh.name = "terrain1"; GetComponent(MeshCollider).sharedMesh = mesh; <-- This doesn't work
I am having a same problem, I can't find any help in the documentation.
Answer by qJake · Apr 22, 2010 at 09:06 PM
Assigning a Mesh Collider to a game object procedurally is relatively easy.
This will assign a Mesh Collider to the mesh. Note that this has to be done AFTER you make and apply your mesh, so stick it at the bottom!
// C# MeshCollider meshc = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
meshc.sharedMesh = terrain1; // Give it your mesh here.
That's it! Unity will calculate and apply your mesh collider here.
Probably not quite his issue, this is about the same of what he's already doing... Look at my answer.
Answer by pkamat · Oct 05, 2010 at 09:48 AM
if you procedurally update the mesh collider u need to Null the mesh before assigning the new mesh. i dont know why but i think unity does some optimisation inside.
GetComponent<MeshCollider>().sharedMesh = null;
GetComponent<MeshCollider>().sharedMesh = meshFilter.mesh;
that should work
GetComponent<$$anonymous$$eshCollider>().shared$$anonymous$$esh = null;
GetComponent<$$anonymous$$eshCollider>().shared$$anonymous$$esh = meshFilter.shared$$anonymous$$esh;
Change the last line to work only with shared$$anonymous$$esh and it should work.
I have procedural mesh applied to meshFilter.mesh and after the mesh has been applied, I then apply the meshFilters shared mesh to the mesh colliders shared mesh.
Answer by Jessy · Apr 05, 2010 at 10:52 PM
What you're trying to do with mesh.name doesn't work. I don't know where you're grabbing "terrain1" from, but here's what you'd do if it were in your project folder. (You'd drag it onto the variable slot. But if you were doing that, I don't see why you wouldn't just add the necessary components in the Editor. Regardless, this is the general idea.)
var terrain1 : Mesh;
(gameObject.AddComponent(MeshFilter) as MeshFilter).mesh = terrain1; gameObject.AddComponent(MeshRenderer); gameObject.AddComponent(MeshCollider);
Very well observed... When I read it, I thought he was just na$$anonymous$$g the mesh for w/e reason.
Answer by DaveM · Apr 06, 2010 at 12:19 AM
Thanks for your help but I'm still not getting any collisions. I don't think that I can just drag from the project folder because the mesh isn't generated until runtime so there is nothing to drag.
Here's more of the code:
var terrain1 : Mesh; (gameObject.AddComponent(MeshFilter) as MeshFilter).mesh = terrain1; gameObject.AddComponent(MeshRenderer); gameObject.AddComponent(MeshCollider);
// Build vertices and UVs
........
// Assign them to the mesh
terrain1.vertices = vertices; terrain1.uv = uv;
// Build triangle indices
........
// And assign them to the mesh
terrain1.triangles = triangles; terrain1.RecalculateNormals();
It's a lot easier to read if you select your code and hit the code formatting button.
And it's a lot better if you only answer your question if you indeed have an answer. What you posted here would make more sense if you edited the question ins$$anonymous$$d.
Answer by Ent · Oct 26, 2010 at 01:10 PM
Hi I am having a similar problem with my created meshes too. If I replace them with box colliders they work straight away. I am combining some meshes from single GameObjects together and so creating a new object.
heres my code:
public GameObject elementObj; public string modelName = ""; ...
this.elementObj = GameObject.Instantiate(Resources.Load("models/"+modelName),Vector3.zero,Quaternion.identity) as GameObject;
this.elementObj.AddComponent<MeshRenderer>();
Component[] meshFilters = elementObj.GetComponentsInChildren(typeof(MeshFilter)); CombineInstance[] combine = new CombineInstance[meshFilters.Length]; for (int i = 0; i < meshFilters.Length; i++) { combine[i].mesh = ((MeshFilter) meshFilters[i]).sharedMesh; combine[i].transform = ((MeshFilter) meshFilters[i]).transform.localToWorldMatrix; } Component frameBoxMesh = frameBox.GetComponent(typeof(MeshFilter)); ((MeshFilter)frameBoxMesh).mesh = new Mesh(); ((MeshFilter)frameBoxMesh).mesh.CombineMeshes (combine,true,true);
MeshCollider frameBoxColl = frameBox.AddComponent(typeof(MeshCollider)) as MeshCollider; frameBoxColl.sharedMesh = null; frameBoxColl.sharedMesh = ((MeshFilter)frameBoxMesh).mesh;
maybe someone else can say why such created meshes are not clickable or why mouse-over does not work. Like mentioned, if I replace the mesh in runtime with a box collider mesh, the scripts work instantly. Do these new created mesh have to be reloaded or something?