Create MeshCollider by script (knowledge question)

Hello everyones,

(I think i’m middle level programming)
My scenario:
In my game, for level design variation and creation facilities, i try to create a proccess that allow me deforming my mesh (ground plateforms)

So i create plateforms with rigg, deform them, and create a new collider mesh using this script(just setting the bool set at true in edit mode):

using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class MeshColliderCreator : MonoBehaviour
{
    public SkinnedMeshRenderer meshRenderer;
    public MeshCollider collider;

    public bool set = false;

    private void Update()
    {
        if(set)
        {
            set = false;
            Mesh colliderMesh = new Mesh();
            meshRenderer.BakeMesh(colliderMesh);
            collider.sharedMesh = null;
            collider.sharedMesh = colliderMesh;
        }
    }
}

That look working perfectly,(even: unity saved and restarted, same model past multiples time and with different deformation) but i just want to understand some things;

  • Is there any risk on doing that ?
  • Is my new meshcollider is stored somewhere ? (that’s show a mesh without name in collider variable)
  • How unity manage this kind of work ?
  • What it cost ? Game weight?

Thx for helping me validating my process, and increasing my knowledge!

I specify that my game is low poly style, and plateforms with mesh colliders is not a problem, and at least a feature of the game.

Well after few days of using, it look working as i said

BUT

Their is strange clue about the (not) saving of the new mesh, if their is on a scene their is no problems, but if i put it as prefab the link to this intangible mesh is lost. Then i decided, for the moment, to create it in awake call. maybe it could be changed in start call.

Hope this help somebodies :wink: and if anybodies want to precises how it work, do it! :wink: