Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by bromley · Dec 21, 2016 at 06:15 PM · editorserializedpropertypropertyfield

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.

Comment
Add comment · Show 1
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 Masterio · Dec 21, 2016 at 06:25 PM 0
Share

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 ;)

1 Reply

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

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.

Comment
Add comment · Show 1 · 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 bromley · Dec 22, 2016 at 05:33 PM 0
Share

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 ();
 }

}

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.

Follow this Question

Answers Answers and Comments

73 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a PropertyField() for a Texture2D SerializedProperty looking like an ObjectField() 2 Answers

ObjectField functions ignoring parameter 1 Answer

Display Custom Inspectors for each class in a List<> 1 Answer

Editor: How to do PropertyField for List elements? 4 Answers

Assigning an object to a SerializedProperty 1 Answer


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