Make procedural mesh collider?

Right, so I’ve seen SO MANY SOLUTIONS to this problem, but I guess I’m just being hated today. What’s even worse, I got it working earlier, but then I had to fix some minor gameObject leak, and THEN IT WAS GONE

sob sniff

What I’m doing is combining all ‘similar’ meshes into one mesh. Not sure if it increases my framerate or not, but it’s worth a go. I mean, it cuts my amount of draw calls in half, so that’s great. However, they (I mean my new meshes) have no physical presence. Nothing wants to collide with them. It’s a tragedy, really, because I finally got the darn thing working.

Basically, I’m trying this solution:
http://answers.unity3d.com/questions/7130/how-to-assign-procedural-mesh-to-a-collider

This is my version of it.

// Add collisions
		MeshCollider meshc = newLevelObject.AddComponent( typeof(MeshCollider) ) as MeshCollider;
		meshc.sharedMesh = newLevelObject.GetComponent<MeshFilter>().mesh; //WHY WON'T YOU WORK YOU PIECE OF SH*T LINE OF CODE

Not much difference, except for the comments.

My whole thing is pretty much this:

	private void OptimizeMeshObject ( GameObject currentProp, GameObject [] objectList, basicLevelBehavior prop )
	{
		int		targetRoom = prop.targetRoom;
		int		i;
		
		basicLevelBehavior	currentBehavior;
		GameObject			currentObject;
		
		// First add current prop to the new gameobject
		GameObject			newLevelObject = new GameObject( "Level Object:"+targetRoom, typeof( MeshFilter ), typeof( MeshRenderer ), typeof( basicLevelBehavior ) );
		newLevelObject.GetComponent<MeshFilter>().mesh = new Mesh();
		newLevelObject.renderer.sharedMaterial = currentProp.renderer.sharedMaterial;
		newLevelObject.GetComponent<basicLevelBehavior>().targetRoom = targetRoom;
		newLevelObject.layer = currentProp.layer; // Don't forget to set the actor collision layers!
		AddToMesh( newLevelObject, currentProp );
		
		// Go through all the objects again
		for ( i = 0; i < objectList.Length; i += 1 )
		{
			currentObject = objectList[i];
			if (( currentObject != null )( currentObject != currentProp ))
			{
				currentBehavior = currentObject.GetComponent<basicLevelBehavior>( );
				if ( currentBehavior != null )
				{
					if ( currentBehavior.targetRoom == targetRoom )
					{
						if ( newLevelObject.renderer.sharedMaterial == currentObject.renderer.sharedMaterial )
							AddToMesh( newLevelObject, currentObject );
					}
				}
			}
		}
		
		// Remove source
		DestroyImmediate( currentProp );
		
		// Add collisions
		MeshCollider meshc = newLevelObject.AddComponent( typeof(MeshCollider) ) as MeshCollider;
		meshc.sharedMesh = newLevelObject.GetComponent<MeshFilter>().mesh; //WHY WON'T YOU WORK YOU PIECE OF SHIT LINE OF CODE
	}
	
	private void AddToMesh ( GameObject targetProp, GameObject sourceProp )
	{
		Mesh	newMesh = new Mesh( );
		
		if ( targetProp.GetComponent<MeshFilter>() == null )
			return;
		
		Mesh	originalMesh = targetProp.GetComponent<MeshFilter>().mesh;
		
		if ( sourceProp.GetComponent<MeshFilter>() == null )
			return;
		
		Mesh	sourceMesh = sourceProp.GetComponent<MeshFilter>().mesh;
		
		if ( sourceMesh == null )
			return;
		
		CombineInstance [] combine = new CombineInstance[ 2 ];
		
		combine[0].mesh = originalMesh;
		combine[0].transform = targetProp.transform.localToWorldMatrix;
		combine[1].mesh = sourceMesh;
		combine[1].transform = sourceProp.transform.localToWorldMatrix;
		
		newMesh.CombineMeshes( combine, true, true );
		
		targetProp.transform.position = Vector3.zero;
		targetProp.transform.rotation = Quaternion.identity;
		targetProp.transform.localScale = Vector3.one;
		targetProp.GetComponent<MeshFilter>().mesh = newMesh;
		targetProp.GetComponent<MeshFilter>().mesh.RecalculateBounds();
		//targetProp.GetComponent<MeshFilter>().mesh.Optimize();
		
		// Destroy source
		DestroyImmediate( sourceProp );
	}

Yeah it’s ugly, but paste it into some external editor and it’ll look a lot more pretty.

Leaving the old colliders there isn’t really an option, because the guns and footsteps and effects and all that look to the render material, not the physical material - dumb move I know - and that system wouldn’t be exactly fun to switch about - arg all the objects I’d have to give different physics materials - and so I would love to get this working.

Summary of Problem:
Procedural mesh created with CombineMeshes seem to not get assigned to the collider.

Any help would be much appreciated.

If the object that has the mesh collider is a rigidbody object, you may need to destroy and re-add the Rigidbody component to get it to recognise the new collider:-

// After sharedMesh is assigned...
Destroy(newLevelObject.rigidbody);
newLevelObject.AddComponent(Rigidbody);
// Set up rigidbody parameters...

If it’s not a rigidbody object, try pausing the game, switching back to the scene view and selecting the object that should have the mesh collider added. You should be able to see the collider mesh as a green wireframe in the scene. Check that it is appearing in registration with the visible mesh.

Well, it’s not a rigidbody object. This is all the level collision. Also, no green mesh appears, even when I select the procedurally generated mesh from the drop down list. That also holds true for all other procedural meshes.

I’m attaching the script I’m using right now. Like I said, everything works as I want it except for the collisions.

Thanks for the attention so far, though.

359026–12469–$gameoptimizelevel_849.cs (6.35 KB)

Isn’t there a way to just tell Unity to generate a collider for a mesh besides trying to rely on the sharedMesh property?