I receive this error:
When I create a new Material in the OnEnable() method on an Editor script in Unity. Does anyone have any suggestions on how to fix the error?
Here’s the OnEnable method that is called on the class that is set as [ExecuteInEditMode]:
public Material materialForLayer;
protected void OnEnable ()
{
ProceduralMesh PM = GetComponentInChildren<ProceduralMesh>();
if(PM != null)
{
MeshRenderer tilesetRenderer = PM.GetComponent<MeshRenderer>();
Material [] tileSetMaterials = tilesetRenderer.sharedMaterials;
Texture textureOnMaterial = null;
if(tileSetMaterials != null tileSetMaterials.Length > 0 )
{
int length = tileSetMaterials.Length;
for (int i = 0; i < length; ++i)
{
//Save the texture of the current material we're iterating on
textureOnMaterial = tileSetMaterials[i].mainTexture;
//The Line of Code that causes the editor error
tileSetMaterials[i] = new Material(materialForLayer);
tileSetMaterials[i].mainTexture = textureOnMaterial;
tileSetMaterials[i].color = materialColor;
}
tilesetRenderer.sharedMaterials = tileSetMaterials;
}
}
}