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

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 :slight_smile: