• 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
Question by security18 · Mar 16, 2016 at 12:38 PM · c#editoreditor-scriptingpropertydrawerproperty

PropertyDrawer let's dissapear my INT

Hello,

I've made a propertydrawer that shows my int as a drop down box, t$$anonymous$$s works fine. Only the problem is sometimes (after being in playmode) the dropdown disappears. T$$anonymous$$s is used to show the drop down:

 [MyProperty..]
 public int StartMenu;

And to get the dropdown i use:

 [CustomPropertyDrawer(typeof(MyProp))]
 public class MyClassDrawer : PropertyDrawer
 {
     private string[] menus;
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         RegisterMenus();
 
         if (property == null)
             return;
 
         if (property.propertyType == SerializedPropertyType.Integer)
         {
             int current = property.intValue;
 
             if (position == null || menus == null)
                 return;
 
             if (menus == null)
                 return;
 
             current = EditorGUI.Popup(position, "Menu to open", current, menus);
 
             property.intValue = current;
         }
     }
 
     private void RegisterMenus()
     {
         if (MenuManager.instance == null)
             return;
 
         List<Menus> tempList = new List<Menus>();
 
 
         foreach (Menus menu in MenuManager.instance.menus)
         {
             if (menu != null)
             {
                 if (menu.menu != null)
                 {
                     if (!tempList.Contains(menu))
                         tempList.Add(menu);
                 }
             }
         }
 
         menus = new string[tempList.Count];
 
 
         for (int i = 0; i < tempList.Count; i++)
         {
             menus[i] = tempList[i].menuName;
         }
     }
 }

In the Menumanager.instance i have a list with classes that contains their names and gameobjects. (They are public so you can set them by hand).

Thanks in advance!!

Comment

People who like this

0 Show 3
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 Baste · Mar 16, 2016 at 02:16 PM 0
Share

When you go to and from play mode, all objects are deleted and recreated. Sometimes, the selected object is not updated properly, meaning that the selected object will be the deleted one from play mode.

If that's what's happening, it's this line that's kicking in:

 if (property == null)
     return;

And the dropdown will reappear if you deselect the object and select it again. Is that the case?

avatar image Bunny83 · Mar 16, 2016 at 03:20 PM 0
Share

I guess that the Menumanager.instance has become null. If it's a single static field, then this will be the case as when entering / leaving playmode static fields are usually erased.

The problem most likely is not in your propertydrawer. A propertydrawer is never called with any of it's parameters being null. Also "position" is a Rect and therefore a value type. It can't be null. The null check most likely will create garbage as the rect has to be boxed.

Also your "menus" null check is redundant (line 17 and 20).

It's good that you have many consistency checks. However this is editor code which should work 100% the time unless something is wrong. If something is wrong you should at least use Debug.LogWarning / LogError to inform the user what's wrong.

So your check inside RegisterMenus could look like this:

 if (MenuManager.instance == null)
 {
     Debug.LogWarning("MenuManager.instance == null");
     return;
 }

This log, if it's hit will also contain a stacktrace so you or the user can see where the problem was raised.

avatar image security18 Bunny83 · Mar 16, 2016 at 09:24 PM 0
Share

Thanks for the response!

Indeed the menumanager is null, i've added a log warning now. I've removed the == null for the property only now is my question how to prevent it from being null?

Thansk in advance!

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trouble setting the object reference in a property drawer 0 Answers

Custom Editor script to show non-native class in inspector 1 Answer

Editor Script, Index Out of Range Exception after Play 1 Answer

Can't use DecoratorDrawer with ScriptableObject in Unity 1 Answer

Custom property drawer for generic 0 Answers


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