- Home /
Is it possible to draw gizmos when an object is selected, but not when the parent is selected?
When one of the enemies in my game is selected, I'd like to draw a gizmo in the scene displaying the area around it where it will detect the player. However, when a parent object of an enemy is selected, I don't want it to draw the detection area gizmos of the child enemies. Is this possible? Is there a way to determine what specific object(s) are selected from within OnDrawGizmosSelected?
Answer by PeterMu · Dec 14, 2015 at 11:49 AM
This worked for me:
using UnityEditor;
if (Selection.activeGameObject != transform.gameObject) {
return;
}
Answer by koirat · Oct 23, 2018 at 05:25 PM
This shows gizmos for selected objects only.
if (!UnityEditor.Selection.gameObjects.Contains(transform.gameObject)) { return; }
Trying this on Unity 2019 doesn't work. UnityEditor.Selection.gameObjects
does not contain a method named Contains
UnityEditor.Selection.Contains(gameObject)
works
You need using System.Linq;
for koirat's solution. Selection.gameObjects is just a GameObject array. But yes, using Selection.Contains would be more efficient since it doesn't allocate any memory.