- Home /
PropertyField problem
I want to show some properties in an inspector only if I select a specific enum index, so if I don't select that enum index these properties will be hidden. I found a script in internet and edited in this way: using UnityEngine; using UnityEditor;
[CustomEditor(typeof(Trigger_DoorState)), CanEditMultipleObjects]
public class TriggerScriptsEditor : Editor {
public SerializedProperty
TriggerInteraction_prop,
itemTrigger_prop,
useItemArea_prop,
switchObject_prop;
void OnEnable () {
TriggerInteraction_prop = serializedObject.FindProperty ("type");
itemTrigger_prop = serializedObject.FindProperty ("pickUpItem");
useItemArea_prop = serializedObject.FindProperty("useItem");
switchObject_prop = serializedObject.FindProperty ("switch");
}
public override void OnInspectorGUI() {
serializedObject.Update ();
EditorGUILayout.PropertyField(TriggerInteraction_prop);
Trigger_DoorState.InteractionType st = (Trigger_DoorState.InteractionType)TriggerInteraction_prop.intValue;
switch( st ) {
case Trigger_DoorState.InteractionType.OnPickUpItem:
EditorGUILayout.PropertyField( itemTrigger_prop, new GUIContent("pickUpItem") );
break;
case Trigger_DoorState.InteractionType.OnUseItem:
EditorGUILayout.PropertyField( useItemArea_prop, new GUIContent("useItem") );
break;
case Trigger_DoorState.InteractionType.OnSwitch:
EditorGUILayout.PropertyField( switchObject_prop, new GUIContent("switch") );
break;
}
serializedObject.ApplyModifiedProperties ();
}
}
Anyway, this script doesn't work and an error message appears:
NullReferenceException: Object reference not set to an instance of an object UnityEditor.PropertyHandler.OnGUILayout (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Editor/Mono/ScriptAttributeGUI/PropertyHandler.cs:190) UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:7296) UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:7278) TriggerScriptsEditor.OnInspectorGUI () (at Assets/Editor/TriggerScriptsEditor.cs:23) UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1235) UnityEditor.DockArea:OnGUI()
Please help me, i don't understand the problem.
try read this and implement it.
https://unity3d.com/learn/tutorials/topics/interface-essentials/building-custom-inspector
In this example you have accs to all LevelScript fields by the myTarget handler.
Next add case with functions what you want.
Hope I helped ;)
Answer by Adam-Mechtley · Dec 21, 2016 at 06:28 PM
Your problem is on line 23 of TriggerScriptsEditor.cs. I don't know if your line numbers here match up exactly, but juts verify in OnEnable that each of those SerializedProperty fields you initialize is not null.
you're right and i'm stupid... in FindProperty i wrote wrong strings here the correct script
using UnityEngine; using UnityEditor;
[CustomEditor(typeof(Trigger_DoorState)), CanEdit$$anonymous$$ultipleObjects] public class TriggerScriptsEditor : Editor {
public SerializedProperty
TriggerInteraction_prop,
itemTrigger_prop,
useItemArea_prop,
switchObject_prop;
void OnEnable () {
TriggerInteraction_prop = serializedObject.FindProperty ("TriggerInteraction");
itemTrigger_prop = serializedObject.FindProperty ("itemTrigger");
useItemArea_prop = serializedObject.FindProperty("useItemArea");
switchObject_prop = serializedObject.FindProperty ("switchObject");
}
public override void OnInspectorGUI() {
serializedObject.Update ();
EditorGUILayout.PropertyField(TriggerInteraction_prop);
Trigger_DoorState.InteractionType st = (Trigger_DoorState.InteractionType)TriggerInteraction_prop.intValue;
switch( st ) {
case Trigger_DoorState.InteractionType.OnPickUpItem:
EditorGUILayout.PropertyField( itemTrigger_prop, new GUIContent("itemTrigger") );
break;
case Trigger_DoorState.InteractionType.OnUseItem:
EditorGUILayout.PropertyField( useItemArea_prop, new GUIContent("useItemArea") );
break;
case Trigger_DoorState.InteractionType.OnSwitch:
EditorGUILayout.PropertyField( switchObject_prop, new GUIContent("switchObject") );
break;
}
serializedObject.Apply$$anonymous$$odifiedProperties ();
}
}