Empty mesh when using combined mesh with MeshCollider

I’ve got an issue with using combined meshes with a MeshCollider. I’ve got a script that takes multiple meshes from mesh filters in child objects, combines them and then passes them to a Mesh Collider. The issue is that the resulting mesh ends up basically not existing. Nothing collides with the expected result in-game and the collision mesh itself doesn’t show up in the editor while the game is paused.

I’m thinking it might be a timing issue, since I also have a script that goes through and activates/deactivates a number of objects based on certain set parameters. However I’d like a second opinion on that.

Oh, and my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class combineCollisionMeshes : MonoBehaviour
{
    void Start()
    {
        MeshFilter[] meshColliders = GetComponentsInChildren<MeshFilter>();
        CombineInstance[] combine = new CombineInstance[meshColliders.Length];
    
        for (int i = 0; i < meshColliders.Length; i++)
        {
            Debug.Log(meshColliders.Length);
            combine[i].mesh = meshColliders[i].sharedMesh;
            combine[i].transform = meshColliders[i].transform.localToWorldMatrix;
            Debug.Log(combine.Length);
        }
    
        Mesh collisionData = new Mesh();
        collisionData.CombineMeshes(combine);
        collisionData.Optimize();
    
        transform.GetComponent<MeshCollider>().sharedMesh = collisionData;
        transform.gameObject.SetActive(true);
    }
}

It’s a bit of a variant on the mesh.CombineMeshes example code, with a setup that allows it to further manipulate the resulting combined mesh before passing it to. I had read in another, much older thread that optimizing the combined mesh will sometimes fix the problem and decided to try it. Obviously it didn’t in this case. :frowning:

The reason I’m doing this is because I have two separate collision meshes that are causing “trip” collisions on a passing rigidbody object where they overlap (seriously, Unity’s collision system needs some serious tweaking. When SM64, a game from 1997, handles collisions better than your engine you know you have a problem) and I couldn’t find a way to fix that with just the existing meshes.

Not an expert here, but shouldn’t you call Mesh.RecalculateBounds in the combined collider?

I’ll try that.

EDIT: Well, it actually worked. But the mesh is rotated incorrectly. That’s a much easier fix, though.

EDIT 2: Just a bit of an update. Unfortunately, despite the script working now, this didn’t fix the issue that it was supposed to fix. I’ll make a separate thread on that, though.