I have trouble with the ExecuteInEditMode Attribute is driving me crazy. Basicly I want to preview a procedural mesh while in editmode and playmode, so a new meshrenderer gameobject should be instantiated and reffered by the script to alter the meshdata (Just like the built-In trailrenderer).
This works so far in both modes, but after every compiling or leaving from Playmode a new meshrender object gets created. How can I maintain for the whole lifetime of the script having only referring the same meshrenderer?
public class CreateMesh : MonoBehaivour {
[SerializeField] private Mesh mesh;
void OnEnable(){
if(meshRenderer == null && !Application.isCompiling && !Application.isPlaying) //STILL CALLS
CreateMeshRenderer();
}
private void CreateMeshRenderer(){
meshRenderer = new GameObject("Mesh_"+name, typeof(MeshRenderer), typeof(MeshFilter)).GetComponent<MeshRenderer>();
mf = meshRenderer.GetComponent<MeshFilter>();
}
This is also called on stored Prefabs in the ProjectWindow.