- Home /
EditorGUILayout.EnumPopup in another script
Hello all I come with a great request for oyu, I make a item system, and I need your help for a little problem, in my ItemDatabaseManager class I have a enum:
public enum TypeOfWeapon
{
baton,
hache1M,
hache2M,
epee1M,
epee2M,
masse1M,
masse2M,
distance,
bouclier,
}
public TypeOfWeapon currentWeaponTypeToCreate = TypeOfWeapon.baton;
and in my ItemManagerInspector class I want make a EnumPopup for modify the type of my weapon.
how to use the public enum in my other class?
Thank.
private ItemDatabase$$anonymous$$anager.TypeOfWeapon myWeapon;
myWeapon = EditorGUILayout.EnumPopup(myWeapon); <- Assets/script/Editor/Item$$anonymous$$anagerInspector.cs(67,33): error CS0266: Cannot implicitly convert type System.Enum' to
ItemDatabase$$anonymous$$anager.TypeOfWeapon'. An explicit conversion exists (are you missing a cast?)
You need to cast it
myWeapon = (ItemDatabase$$anonymous$$anager.TypeOfWeapon) EditorGUILayout.EnumPopup(myWeapon);
I have an error: NullReferenceException: Object reference not set to an instance of an object Item$$anonymous$$anagerInspector.OnInspectorGUI () (at Assets/script/Editor/Item$$anonymous$$anagerInspector.cs:25) UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean forceDirty, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect, Boolean eyeDropperDirty) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/$$anonymous$$ono/Inspector/InspectorWindow.cs:1124) UnityEditor.DockArea:OnGUI()
Answer by PixelFireXY · Aug 29, 2014 at 03:51 PM
for manage your public field from another class, you can use:
[CustomEditor(typeof(ItemDatabaseManager))]
public class ItemManagerInspector : Editor {
public override void OnInspectorGUI()
{
ItemDatabaseManager item = (ItemDatabaseManager)target;
TypeOfWeapon itemType = item.itemType;
itemType = (TypeOfWeapon)EditorGUILayout.EnumPopup("Item type", itemType);
item.itemType = itemType;
}
}
Thanks but this problem was solved, new problem: when I chose the type of my weapon all my weapon are with this type
myWeapon = (ItemDatabase$$anonymous$$anager.TypeOfWeapon)EditorGUILayout.EnumPopup(myWeapon);
weapon[i].slot = myWeapon.ToString();
How to use this with index of my weapon List?
Use my lines of code, i solved the problem as i told you. In your way, you initialize your field every time with last param, and then all values are the same, also for edit the value from another script you can use:
public class ItemDatabase$$anonymous$$anager : $$anonymous$$onoBehaviour {
public TypeOfWeapon itemType;
private TypeOfWeapon LastitemType;
void Update()
{
if(LastitemType != itemType)
{
LastitemType = itemType;
weapon[i].slot = itemType.ToString();
}
}
}
i can't help you with just "don't work", please be more detailed.
$$anonymous$$aybe you don't have enough skill for this, try to follow this tutorial, it maybe can help you:
http://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector
I know how to create a custom editor, but your code don't work: all weapon take the same enum
Answer by M-G-Production · Mar 06, 2015 at 07:09 PM
Salut Isador34! Je suis passé par le même problème...
So to display an EnumPopup on your custom inspector, put this line of code in your OnInspectorGUI() function:
//Look for the Enum ("ItemDatabaseManager") and the var you want to change/display ("currentWeaponTypeToCreate")
ItemDatabaseManager.currentWeaponTypeToCreate = (ItemDatabaseManager)EditorGUILayout.EnumPopup("The Weapon: ", ItemDatabaseManager.currentWeaponTypeToCreate);
And finally, add "static" before currentWeaponTypeToCreate in your ItemDatabaseManager class:
public static TypeOfWeapon currentWeaponTypeToCreate = TypeOfWeapon.baton;
Hope this helps! Math
Your answer
Follow this Question
Related Questions
Type cast to a enum? 1 Answer
Creating a unity insepctor dropdown menu with content 0 Answers
EditorGUILayout.EnumPopup not found 1 Answer
SerializedProperty for Enum with EnumPopup 6 Answers
fftWindow isn't enum C#? 1 Answer