- Home /
Override Transform Inspector using SerializedObject and SerializedProperty
Hi everyone!
I'm trying to override the default Transform inspector component to add some functionality using SerializedObject and SerializedProperty but I'm not able to get the properties from the native transform like position, localPosition, scale...
The following code show what I'm trying to do, I did the same thing with a custom script that I've here and I get the property right, but when I try to get the "position" property from the transform, it returns a null value.
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Transform)), CanEditMultipleObjects]
public class TransformEditor : Editor
{
private SerializedObject m_object;
private SerializedProperty m_propPosition;
private void OnEnable()
{
m_object = new SerializedObject(target);
m_propPosition = m_object.FindProperty("position");
}
public override void OnInspectorGUI()
{
m_object.Update();
EditorGUILayout.PropertyField(m_propPosition);
m_object.ApplyModifiedProperties();
}
}
When I do this I get the error below...
NullReferenceException: Object reference not set to an instance of an object UnityEditor.EditorGUI.GetSinglePropertyHeight (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) ...
Answer by Xroft666 · Oct 22, 2012 at 11:04 PM
Here you go, man :)
m_propPosition = m_object.FindProperty ("m_LocalPosition");
Use "SerializedObject.GetIterator()" + "SerializedProperty.Next()" to check the "propertyPath" to properties in debug mode