Replace MeshFilter mesh by a other mesh in Editor

Hi,

I’ve created a script to replace (in Editor) all the MeshFilter’s mesh in my scene by the mesh “meshToAssign” but i’m having trouble to get my mesh list.

Here is my code :

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

public class ReplaceMesh : MonoBehaviour
{
    public Mesh meshToReplace;
    public Mesh meshToAssign;

    public List<MeshFilter> meshFilterToWorkOn;

    [ContextMenu("FindMeshRendererToWorkOn")]
    public void FindMeshRendererToWorkOn()
    {
        meshFilterToWorkOn.Clear();

        MeshFilter[] meshFilterAll = FindObjectsOfType<MeshFilter>();
        int meshFilterAllLength = meshFilterAll.Length;
        for (int i = 0; i < meshFilterAllLength; i++)
        {
            if (meshFilterAll*.sharedMesh == meshToReplace)*

meshFilterToWorkOn.Add(meshFilterAll*);*
}
}

[ContextMenu(“ReplaceAllMeshInList”)]
public void ReplaceAllMeshInList()
{
int meshFilterToWorkOnCount = meshFilterToWorkOn.Count;
for (int i = 0; i < meshFilterToWorkOnCount; i++)
meshFilterToWorkOn*.mesh = meshToAssign;*
}
}
It seems like the variable “meshToReplace” isn’t equal to “meshFilterAll.sharedMesh” but when i tryed with “meshFilterAll_.mesh” a error has appear in the console when i execute the command.
Can you guys help me ?_

It wasn’t equal to sharedMesh cause meshToReplace was a assetMesh and not a sharedMesh.
But it’s working now ! I just had to get the sharedMesh of my MeshFilter.

public class ReplaceMesh : MonoBehaviour
{
    public MeshFilter meshToReplace;
    public Mesh meshToAssign;

    public List<MeshFilter> meshFilterToWorkOn;

    [ContextMenu("FindMeshRendererToWorkOn")]
    public void FindMeshRendererToWorkOn()
    {
        Mesh sharedMesh = meshToReplace.sharedMesh;
        meshFilterToWorkOn.Clear();

        MeshFilter[] meshFilterAll = FindObjectsOfType<MeshFilter>();
        int meshFilterAllLength = meshFilterAll.Length;
        for (int i = 0; i < meshFilterAllLength; i++)
        {
            if (meshFilterAll*.sharedMesh == sharedMesh)*

meshFilterToWorkOn.Add(meshFilterAll*);*
}
}

[ContextMenu(“ReplaceAllMeshInList”)]
public void ReplaceAllMeshInList()
{
int meshFilterToWorkOnCount = meshFilterToWorkOn.Count;
for (int i = 0; i < meshFilterToWorkOnCount; i++)
meshFilterToWorkOn*.mesh = meshToAssign;*
}
}
Thanks for u reply !