- Home /
Prevent ExecuteInEditMode scripts to call Awake OnEnable Start after compiling and game exit
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.
Answer by toficofi · Mar 21, 2021 at 01:26 PM
Do you want to preview the mesh only when the GameObject is selected in Scene view? If so, you could use an Update() instead and check to see if the object is contained in Selection.transforms.
The problem does not rely in when to preview, it is in duplications of the meshrenderer gameobject per script instance everytime the compiler runs or the game exits. The mesh itself updates when a variable is changed via OnValidate callback.
I understand. I am suggesting that you instead create the mesh renderer when the GameObject has entered Selection.transforms (when it's been selected) and destroy the renderer once it has left the selection. Then you don't need to care about OnEnable at all
Your answer
![](https://koobas.hobune.stream/wayback/20220613031033im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Display a procedural mesh in the editor 0 Answers
Weird issue with mesh.Clear() 1 Answer
How to prevent editor to set the scene dirty if a script changes a mesh (@ExecuteInEditMode) 1 Answer
A way to know if unity was just opened? [ExecuteInEditMode] 0 Answers
Sphere made of cubes algorithm 4 Answers