- Home /
Problem with second enum selection inspector
I have gotten the first selaction of an enum to work dynamically show some of the values that will be connected to that enum in the inspector. And I'm been able to expose int values, but not a second enum value. Based on this link: https://answers.unity.com/questions/417837/change-inspector-variables-depending-on-enum.html
So i got this part working, but i want to add a secondary selaction like the first one i created under the first one. but i am keep getting NullReferenceException. I am very new to inspector and editor scripting, have mostly just coded with MonoBehaviour and would have an easier time trying to find the issue.
Edit: Forgot to mention, but the error is specific to the line:
EditorGUILayout.PropertyField(itemType_Property);
MonoBehaviour script:
public class ObjectProperty : MonoBehaviour
{
public enum ObjectType { human ,vehicle, item, weapon, door };
public ObjectType objectType;
public int actionAmount = 0;
[HideInInspector] public GameObject meshGameObject;
[HideInInspector] public GameObject thisGameObject;
//WeaponSystem Info -------------------------------------
[HideInInspector] public UnitItem weaponItem;
[System.Serializable]
public class WeaponStats
{
public float bulletSpeed = 1f;
public float rateOfFire = 1f;
public GameObject gunBarrelEnd;
public GameObject weaponGrip;
public GameObject secondaryWeaponGrip;
public GameObject weaponMeshGameObject;
public ProjectileSystem.AmmoList caliberType;
}
[System.Serializable]
public class ItemStats
{
public string weaponName;
public UnitItem.ItemType itemType;
public int itemCount = 1;
public int weightModifier;
public int itemWeight = 1;
public float healthModifier;
public float shieldModifier;
public float armorModifier;
}
public WeaponStats weaponStats;
public ItemStats itemStats;
Editor script:
[CustomEditor(typeof(ObjectProperty))]
public class ObjectPropertyEditor : Editor
{
public SerializedProperty actionAmount_Property;
public SerializedProperty objectType_Property;
public SerializedProperty weaponStats_Property;
public SerializedProperty itemStats_Property;
//weaponStats
//itemStats
public SerializedProperty weaponName_Property;
public SerializedProperty itemType_Property;
public SerializedProperty itemCount_Property;
public SerializedProperty weightModifier_Property;
public SerializedProperty itemWeight_Property;
public SerializedProperty healthModifier_Property;
public SerializedProperty shieldModifier_Property;
public SerializedProperty armorModifier_Property;
void OnEnable()
{
actionAmount_Property = serializedObject.FindProperty("actionAmount");
objectType_Property = serializedObject.FindProperty("objectType");
weaponStats_Property = serializedObject.FindProperty("weaponStats");
itemStats_Property = serializedObject.FindProperty("itemStats");
//weaponStats
//itemStats
weaponName_Property = serializedObject.FindProperty("weaponName");
itemType_Property = serializedObject.FindProperty("itemType");
itemCount_Property = serializedObject.FindProperty("itemCount");
weightModifier_Property = serializedObject.FindProperty("weightModifier");
itemWeight_Property = serializedObject.FindProperty("itemWeight");
healthModifier_Property = serializedObject.FindProperty("healthModifier");
shieldModifier_Property = serializedObject.FindProperty("shieldModifier");
armorModifier_Property = serializedObject.FindProperty("armorModifier");
}
override public void OnInspectorGUI()
{
var ObjectProperty = target as ObjectProperty;
// base.OnInspectorGUI();
serializedObject.Update();
EditorGUILayout.PropertyField(objectType_Property);
ObjectProperty.ObjectType chosen = (ObjectProperty.ObjectType)objectType_Property.enumValueIndex;
switch (chosen)
{
case ObjectProperty.ObjectType.human:
{
EditorGUILayout.IntSlider(actionAmount_Property, 0, 10, new GUIContent("actionAmount"));
}
break;
case ObjectProperty.ObjectType.item:
{
EditorGUILayout.IntSlider(actionAmount_Property, 0, 10, new GUIContent("actionAmount"));
}
break;
case ObjectProperty.ObjectType.weapon:
{
EditorGUILayout.IntSlider(actionAmount_Property, 0, 10, new GUIContent("actionAmount"));
EditorGUILayout.PropertyField(itemType_Property);
UnitItem.ItemType chosenItem = (UnitItem.ItemType)itemType_Property.enumValueIndex;
//How to show values from MonoBehaviour
switch (chosenItem)
{
case UnitItem.ItemType.item_HeadGear:
break;
case UnitItem.ItemType.item_Eye:
break;
case UnitItem.ItemType.item_Torso:
break;
case UnitItem.ItemType.item_TorsoArmor:
break;
case UnitItem.ItemType.item_Legs:
break;
case UnitItem.ItemType.item_Backpack:
break;
case UnitItem.ItemType.item_Special:
break;
case UnitItem.ItemType.item_Ammo:
break;
case UnitItem.ItemType.item_Weapon:
break;
case UnitItem.ItemType.item_Generic:
break;
}
break;
}
case ObjectProperty.ObjectType.vehicle:
{
EditorGUILayout.IntSlider(actionAmount_Property, 0, 10, new GUIContent("actionAmount"));
}
break;
case ObjectProperty.ObjectType.door:
{
EditorGUILayout.IntSlider(actionAmount_Property, 0, 10, new GUIContent("actionAmount"));
}
break;
}
serializedObject.ApplyModifiedProperties();
}
}
Edit2: UnitItem class
public class UnitItem : MonoBehaviour
{
public enum ItemType
{
item_HeadGear,
item_Eye,
item_Torso,
item_TorsoArmor,
item_Legs,
item_Backpack,
item_Special,// Special, Jetpack, Grappling Hook, ExoSuit
item_Ammo,
item_Weapon,
item_Generic
}
/*
public string item_HeadGear = "Head gear";
public string item_Eye = "Eye Wear";
public string item_Torso = "Torso";
public string item_TorsoArmor = "Armor/Carrier gear";
public string item_Legs = "Head gear";
public string item_Backpack = "Backpack";
public string item_Special = "Special"; // Special, Jetpack, Grappling Hook, ExoSuit
public string item_Ammo = "Ammo";
public string item_Weapon = "Weapon";
*/
public UnitItem unitItemRef;
public GameObject canvasUIGameObject;
public GameObject inWorldItem;
public float healthModifier;
public float shieldModifier;
public float armorModifier;
public int weightModifier;
public int itemWeight;
public string itemName;
public ItemType itemType;
public UnitItem(GameObject inWorldItem, string itemName, ItemType itemType, int itemCount, int weightModifier, int itemWeight, float healthModifier, float shieldModifier, float armorModifier)
{
this.unitItemRef = this;
this.itemName = itemName + " Item";
this.itemType = itemType;
this.healthModifier = healthModifier;
this.shieldModifier = shieldModifier;
this.armorModifier = armorModifier;
this.weightModifier = weightModifier;
this.itemWeight = itemWeight;
this.canvasUIGameObject = new GameObject(itemName + " Item");
this.canvasUIGameObject.AddComponent<RectTransform>();
this.canvasUIGameObject.AddComponent<CanvasRenderer>();
this.canvasUIGameObject.AddComponent<Image>();
this.canvasUIGameObject.AddComponent<CanvasGroup>();
this.canvasUIGameObject.AddComponent<PlayerHUDInventoryItem>();
this.canvasUIGameObject.GetComponent<PlayerHUDInventoryItem>().unitItem = unitItemRef;
this.canvasUIGameObject.GetComponent<CanvasRenderer>().cullTransparentMesh = true;
this.canvasUIGameObject.GetComponent<Image>().color = new Color32(200, 80, 80, 255);
this.canvasUIGameObject.GetComponent<Image>().raycastTarget = true;
this.canvasUIGameObject.GetComponent<Image>().maskable = true;
this.canvasUIGameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(80, 50);
this.canvasUIGameObject.GetComponent<CanvasGroup>().blocksRaycasts = true;
this.canvasUIGameObject.GetComponent<CanvasGroup>().interactable = true;
this.canvasUIGameObject.GetComponent<CanvasGroup>().alpha = 1;
if (!(inWorldItem == null))
{
this.inWorldItem = inWorldItem;
this.canvasUIGameObject.GetComponent<PlayerHUDInventoryItem>().itemGO = this.inWorldItem;
}
}