private void UpdateVisualMaterial(Material oldMaterial, Material newMaterial) { // If the material has changed and the new material is not null if (newMaterial != oldMaterial && newMaterial != null) { CSGModel csgModel = (CSGModel)target; // Update the built mesh renderers that use the old material Transform meshGroup = csgModel.GetMeshGroupTransform(); if (meshGroup != null) { MeshRenderer[] meshRenderers = meshGroup.GetComponentsInChildren <MeshRenderer>(); for (int i = 0; i < meshRenderers.Length; i++) { if (meshRenderers[i].sharedMaterial == oldMaterial) { meshRenderers[i].sharedMaterial = newMaterial; } } } // Update all the polygons in the source brushes List <Brush> brushes = csgModel.GetBrushes(); for (int i = 0; i < brushes.Count; i++) { if (brushes[i] != null) // Make sure the tracked brush still exists { Polygon[] polygons = brushes[i].GetPolygons(); for (int j = 0; j < polygons.Length; j++) { // If the polygon uses the old material (as an override) if (polygons[j].Material == oldMaterial) { // Set the polygon to null, this removes the material override which gives consistent behaviour polygons[j].Material = null; } } } } } }