- Home /
How to create gameObjects in editor mode
I'm quite new here, sorry if I say anything crazy. So, I have a bunch of data (position, vertices and triangles) saved and what I want to do is create a map using all this data. The think is I only want to run this script once. Using ExecuteInEditMode in Start() would do that every time I open unity, Update() would run it everytime I want to change anything in the scene and that's not what I'm looking for. By now, I have the script in a GameObject called "map". I've tried using the Start() method but using that I have the problem that when assigning the vertices, I get a NullReferenceExeption from the new GameObject. The idea was using it once and then remove the script from the map GameObject. The script I have now it kinda works when it's used without the ExecuteInEditMode, the only problem I have is that the new GameObjects I don't know why, can't get their material changed. But that's another problem. Anyone has any idea of what should I do? Thank you.
Answer by Pangamini · Oct 04, 2021 at 11:21 AM
You may want to write some editor code. It could be a window, or just a static method that gets called from menu. I'd suggest the former, so that you can have some configuration GUI. You'd be creating some sort of import data / level generator wizard. The code wouldn't even exist in the game build, just the editor. Look at EditorWindow. Pro tip: EditorWindow can have serialized fields, just like MonoBehaviours. They are not automatically displayed anywhere (unlike MonoBehaviours), but they are saved in the window layout.
A simple Editor script fixed all my problems! A simple button. Didn't know I could do that. Now that I know, I'm pretty sure I'm going to use that a lot. Thank you.