Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by grimmy · Feb 13, 2012 at 10:51 AM · editor-scriptingselections

How to I force the selection of a parent when a child is selected in the editor?

Lets say I have a parent object with a child attached. Inside the editor the user selects the child object. I want to force the editor to select it's parent.

How do I do that?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
19

Answer by Cameron Petty · Nov 03, 2015 at 05:59 AM

Very old question, but I came upon it tonight when trying to remember the answer.

The attribute may well have been added to the API after this question was asked, for all I know, but for any others that might happen by here, the functionality desired is the SelectionBaseAttribute.

[SelectionBase]

public class PlayerScript : MonoBehaviour {

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image steveh2112 · Feb 09, 2020 at 08:58 AM 0
Share

sorry, where does this code go?

avatar image
5

Answer by oscarsweatman · Mar 03, 2012 at 11:08 AM

I've noticed that if any gameobject is created from a prefab (its text will appear blue in the hierarchy window) Unity will select the parent object if any of the children objects are clicked in the view.

Example: If you add two simple game objects to the scene (sphere, cube, etc) and parent one to the other, Unity will always select the one you click whether it's the parent or child.

If you take that same hierarchy of objects and make a prefab out of it and add that prefab to the scene, Unity will select the parent object (the entire prefab) in the scene.

Also, if you are needing to do this through script you can modify the current selection by using the "Selection" class:

 (C#)
 Selection.activeGameObject = Selection.activeGameObject.transform.parent;

You can find the Selection class documentation here.

Hope that answers your question :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by dmi3iy · Mar 09, 2019 at 04:22 PM

I wrote this little script, based on Help and code above. When you select object in editor window and after that press alt + C, you will get the selected parent of object.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 public class SelectParent : EditorWindow {
 
     [MenuItem("Edit/Select parent &c")]
     static void SelectParentOfObject()
     {
         Selection.activeGameObject = Selection.activeGameObject.transform.parent.gameObject;
        
     }
 }
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image raphax · Mar 14, 2021 at 10:44 AM 1
Share

Great idea! Make it even better with some null checks:

  Selection.activeGameObject = Selection.activeGameObject?.transform.parent?.gameObject;
avatar image xenofusion · Mar 11 at 09:15 PM 0
Share

I ran into this problem today when I wanted to select thousands of trees and delete them but the editor kept selecting children instead of the parents. I built upon your script to make it work for multiple selected objects.

 using System.Collections.Generic;
 using UnityEditor;
 using UnityEngine;
 
 public class SelectParent : EditorWindow
 {
     [MenuItem("Edit/Select Parent &c")]
     static void SelectParentOfObjects()
     {
         List<GameObject> newSelection = new List<GameObject>();
         foreach (var s in Selection.objects)
         {
             newSelection.Add((s as GameObject).transform.parent?.gameObject);
         }
         Selection.objects = newSelection.ToArray();
     }
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to minimize the main editor Window by script 1 Answer

Why would editor script be slow on first compilation? 0 Answers

Help with scrollbar and offset 1 Answer

Detecting scene save - replacement for AssetModificationProcessor.OnWillSaveAssets 1 Answer

A weird issue in Unity related to scripts and Editor. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges