• 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
2
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
6
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 4 · 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.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

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

Edit child in parent list [Custom Inspector] 1 Answer

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

Custom tab of Terrain inspector, is it possible? 0 Answers

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges