• 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
3
Question by ronronmx · Jun 24, 2012 at 07:13 AM · editorinspector

Custom Editor to show child component on parent?

This might be a weird question, so let me explain.

Let's say you have a GameObject (the Parent), which has a child with a BoxCollider set as a trigger. The child has a script attached to it which detects OnTriggerEnter() events, and which has a few public fields that show up in the inspector.

A better place for this script would be on the parent itself, so it's easier to find at first glance, but the parent doesn't have a BoxCollider, and never will. So the script HAS to be on the child.

I'd love to be able to display that script's public fields on the Parent itself, while keeping the script on the child. Is this possible with a custom inspector class? Is it it much simpler then I think it is?

Thanks for your time guys! Stephane

Comment
Add comment · Show 2
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 Fattie · Jun 24, 2012 at 07:44 AM 0
Share

it's a great idea !

avatar image ronronmx · Jun 24, 2012 at 09:04 AM 0
Share

Thanks :) Now I just need to know how to make it work ;P

1 Reply

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by delstrega · Jun 24, 2012 at 10:30 AM

There you go:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
     
       
 [CustomEditor(typeof(TestClass))]
 public class TestClassEditor: Editor {
     
 private TestClass myTestClassInstance;
         
 void OnEnable(){
     myTestClassInstance = (TestClass) target;
 }
     
 public override void OnInspectorGUI () {
         DrawDefaultInspector();
             
     BoxCollider myBoxCol = myTestClassInstance.GetComponentInChildren<BoxCollider>();
         
     if(myBoxCol != null){
         SerializedObject boxSO = new SerializedObject(myBoxCol);
         if(boxSO != null){
         SerializedProperty prop = boxSO.GetIterator();
         while (prop.NextVisible(true)) {
               if(prop.name != "m_Script" && prop.name.StartsWith("m_")){
                          EditorGUILayout.PropertyField(prop);
                }
             }
             prop.Reset();
         }
         boxSO.ApplyModifiedProperties();
     }
     }
 }

Just replace "TestClass" with the name of your parent object's class.

Hope that helps :)

Comment
Add comment · Show 5 · 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 ronronmx · Jun 24, 2012 at 06:38 PM 0
Share

Thanks for the example DelStrega, very helpful and informative :)

avatar image delstrega · Jun 24, 2012 at 07:25 PM 0
Share

Glad I could help :)

avatar image Awix97 · Mar 09, 2020 at 01:20 PM 0
Share

Hey, is there a way to edit fields in many objects at once? I use [CanEdit$$anonymous$$ultipleObjects] but changes are saved on only one object.

avatar image Bunny83 Awix97 · Mar 09, 2020 at 02:30 PM 0
Share

I don't quite understand your question. This whole approach is already dangerous since it's hard to tell for a user where the properties he's editing actually belongs to. Why don't you just select all your child objects you want to edit? That way you're editing all at once and you can actually control which you want to edit and which not without any kind of editor scripting.


This code uses "GetComponentInChildren" which will get the first component on the first selected object. So this approach is extremely misleading for a user. If you read the OPs question he has a very specific setup and only has one script on a certain child object which he wanted to display on the parent. The custom editor also specifically breaks multi editing since that wouldn't work that way.


If you have further questions, please ask a seperate question. This question is 8 years old and already answered. If you ask a seperate question be sure to include more details about your case.

avatar image ClearRoseOfWar · Feb 14 at 04:00 PM 0
Share

Thank you! Much Love!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

The parallel function to Selection.activeObject in the Inspector? 0 Answers

How to get variables of the script show in unity editor/inspector? 2 Answers

How do I implement Draggable Properties with custom Labels in Editor or PropertyDrawer? 1 Answer

How can "bounds" for script variables be defined for use in the Inspector? 2 Answers


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