- Home /
Programatically instantiating GameObjects to the editor (not during gameplay)
Hello, I am working on a tile based tactics game and would like to be able to use code to instantiate my tiles (and all of the data necessary). I know how to do this at run time, but I need to be able to make exceptions to what the general rules on a case by case basis.
Because of this, I would like to be able to, in the editor, programatically instantiate my network of tiles. From there, I could manually tweak the tiles. I want this to happen not during in-editor game play (CTRL P), so that it and my tweaks persist.
Thank you for your time.
Answer by Lucas Meijer 1 · Feb 20, 2010 at 06:05 PM
The syntax to create gameobjects is the same as in runtime. You can either make a custom editor window to facilitate your game object creation, or use a MenuItem:
//Make sure to place this script in a folder called "Editor"
using UnityEditor;
public class MyTools
{
[MenuItem("MyTools/CreateGameObjects")]
static void Create()
{
for (int x=0; x!=10; x++)
{
GameObject go = new GameObject("MyCreatedGO"+i);
go.transform.position = new Vector3(x,0,0);
}
}
}
good luck, Lucas
Seems like what I need. Will let you know how it goes - thanks a ton.
Is there a way to do this without automatically adding the object to the scene?
Line 10 should be GameObject go = new GameObject("$$anonymous$$yCreatedGO"+x);
Answer by Rispat-Momit · Apr 13, 2019 at 08:10 PM
Here is the above script with no errors and with creating the objects through Unity's window GameObject/3D Object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class CreateOBJ : MonoBehaviour {
[MenuItem("GameObject/3D Object/CreateGameObjects")]
static void Create()
{
for (int x = 0; x != 10; x++)
{
GameObject go = new GameObject("MyCreatedGO" + x);
go.transform.position = new Vector3(x, 0, 0);
}
}
}
Your answer

Follow this Question
Related Questions
Generate 2 gameobjects as child 2 Answers
Editor Script: Linking GameObjects to public script variables resets when playing. 1 Answer
in-editor cloning of game objects 1 Answer
Mark gameobject field as changed from prefab 2 Answers
Custom Inspector: Accessing a reference to another MonoBehaviour? 1 Answer