- Home /
Can I draw gizmos when a child is selected?
I would like to render gizmo for my object, but only if it or any of its descendants are selected. As far as I can tell, my choices are to use OnDrawGizmos, which will ALWAYS draw the gizmo, or OnDrawGizmosSelected, which will not draw the gizmo if I select a child. Does anyone know a way to draw a gizmo if an object or any of its descendants are selected?
Answer by GlitchEnzo2 · Dec 05, 2010 at 06:49 AM
I would use the OnDrawGizmos method but check the Selection to only draw the gizmo if your object, or one of its children, is selected.
Edit - You could also use OnDrawGizmosSelected but then add that script to all children objects as well. Unless there is a reason why you cannot add it to the children. In that case, I'd do what I stated above.
The Selection class is an Editor class only, though. If I place my script in the Editor folder, it can no longer be used in-game. Is there a way to separate out my OnDrawGizmos function into the editor, while leaving the class as a non-editor class?
Hmm, I have some questions then. (1) How are you selecting objects outside of the Editor? (2) Why can't you implement OnDrawGizmosSelected and add that script to all children objects?
I'm not selecting objects outside the editor, but every script has to be either an editor script, or a run-time script. I'm writing a spline behaviour that bases the shape of the spline on child transforms, but when I select the child, I no longer see all my spline widgets drawn. I could theoretically add the logic for drawing the spline into the children (and it's what I originally wrote), but it means every time I add a control point to my spline, I have to add script to the control point. It's an extra step just to forward the selected status to the spline that doesn't seem worth it.
Oh I understand what you're saying now. You should be able to separate it out into two scripts. One that implements OnDrawGizmos, uses the Selection class, and resides in the Editor. Another that has all of your run-time code. You then just add each script as a component on your parent object.
That sounds good. I'll try it. For now I just implemented the drawing in OnDrawGizmos, and added a public bool to turn on/off the gizmo drawing.
Answer by B0gy · Nov 26, 2012 at 09:46 AM
You could do this in your child object
void OnDrawGizmosSelected()
{
this.transform.parent.SendMessage("OnDrawGizmosSelected");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613085740im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I force a GameObject with children to collapse and expand in the hierarchy view? 1 Answer
UI - How can I prevent a Rect Transform on a canvas from blocking clicks? 1 Answer
How to structure a project using a lot of asset store assets? 1 Answer
Hierarchy not showing anything, getting KeyNotFoundException 1 Answer