Creating GameObjects, parenting, and registering Undo actions
Whether from a menu item, or an Inspector
view, there are times when we want to create a new GameObject
in the scene from an Editor Extension. In this recipe, we'll create a new GameObject
and set its position and color randomly:

How to do it...
To create an object and change its value follow these steps:
- In the
Project
panel, create a new folder,Editor
. - In your new
Editor
folder create a new C# script-class namedObjectManager.cs
, containing the following:
using UnityEditor; using UnityEngine; public class ObjectManager : EditorWindow { [MenuItem("GameObject/MyObjectManager/Create New Empty Game Object")] static void CreateCustomEmptyGameObject(MenuCommand menuCommand) { GameObject go = new GameObject("GameObject - custom - Empty"); go.transform.position = RandomPosition(5); // Ensure it gets reparented if this was a context click (otherwise does nothing) GameObjectUtility...