Working with selected objects and deactivating menu items
Sometimes, we only want to execute some statements if an object is currently selected, related to those actions. In this recipe, we learn how to disable a menu item if nothing is selected. If a GameObject is selected, we'll get a reference to that object and move it back to the origin (0,0,0):

How to do it...
To work with selected objects and deactivate menu items follow these steps:
- In the
Project
panel, create a new folder,Editor
. - In your new
Editor
folder create a new C# script-class namedSelectedObjectManager.cs
, containing the following:
using UnityEditor; using UnityEngine; public class SelectedObjectManager : EditorWindow { [MenuItem("MyMenu/Move To Origin")] static void ZeroPosition() { GameObject selectedGameObject = Selection.activeTransform.gameObject; Undo.RecordObject (selectedGameObject.transform, "Zero Transform Position"); selectedGameObject.transform.position = Vector3.zero...