- Home /
PropertyDrawer let's dissapear my INT
Hello,
I've made a propertydrawer that shows my int as a drop down box, this works fine. Only the problem is sometimes (after being in playmode) the dropdown disappears. This 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!!
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?
I guess that the $$anonymous$$enumanager.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 Register$$anonymous$$enus could look like this:
if ($$anonymous$$enu$$anonymous$$anager.instance == null)
{
Debug.LogWarning("$$anonymous$$enu$$anonymous$$anager.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.
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!
Your answer
Follow this Question
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
Stop a MonoBehavior from being addable as a script? 0 Answers
Call a function from CustomPropertyDrawer of Arbitrary class 1 Answer
reflection propertyinfo.getvalue compiles fine but gives erros in editor 1 Answer