Search Unity

Help with CustomEditor target as issue when inherriting MonoBehaviour

Discussion in 'Scripting' started by Chance-Touchstone, Jun 13, 2019.

  1. Chance-Touchstone

    Chance-Touchstone

    Joined:
    Dec 26, 2013
    Posts:
    43
    I have this Serializable class called Item that I'm using in a List<> property to display a list of custom items in the inspector. I removed MonoBehaviour from it's inheritance because it didn't display correctly in the inspector with it. Now I'm trying to implement a CustomEditor(typeof(Item) class for some additional OnInspectorGUI() functionality but it has the following error on the line with "= target as Item;":

    Error: Cannot convert type 'UnityEngine.Object' to Item via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion."

    If I re-add the MonoBehaviour inheritance it doesn't give me the error but then the Item doesn't display correctly in the script's inspector.

    Code (CSharp):
    1. [System.Serializable]
    2. public class Item // : MonoBehaviour //ScriptableObject
    3. {
    4.  
    5.     public string Name;
    6.     [HideInInspector]
    7.     public ItemType Type;
    8.     public int Count = 0;
    9.     public int MaxQuantity = 99;
    10.     public bool IsCraftable = false;
    11.     public ItemType[] CraftingRequirements = null;
    12.  
    13.  
    14.     //public Item( etc.. etc.
    15. }
    16.  
    17.  
    18.  
    19. [CustomEditor(typeof(Item))]
    20. public class ItemEditor : Editor
    21. {
    22.     //override public void OnInspectorGUI()
    23.     public override void OnInspectorGUI()
    24.     {
    25.  
    26.         var item_script = target as Item; //This errors
    27.  
    28.       //Error:  Cannot convert type 'UnityEngine.Object' to Item via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion.
    29.      }
    30. }

    And this is how I'm using Item in my property:

    //In another script:
    Code (csharp):
    1.  
    2.     public List<Item> Weapons;
    3.     Weapons.Add(new Item(ItemType.MeleeWeapon));
    4.  

    Q) How can I get ItemEditor to compile without that inheritance or Item to display correctly with it?
     
  2. Chance-Touchstone

    Chance-Touchstone

    Joined:
    Dec 26, 2013
    Posts:
    43
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    "target" refers to the UnityEngine.Object being inspected. In other words, it's the object that's highlighted in the editor, that an inspector is being drawn for (if multiple objects are selected, then it's just the first one in the list). CustomEditors are made for classes the derive from UnityEngine.Object, as that's what's selectable in the editor- since "Item" is not a MonoBehaviour or ScriptableObject, it literally can't be the object being inspected, so instead, the CustomEditor class here needs to be for the MonoBehaviour/ScriptableObject that contains that Item- the one the inspector is actually being drawn for.

    If you're wanting to make a custom drawer for a non-UnityEngine.Object, in other words change how Item instances are displayed in the inspector when any MonoBehaviour is drawing it, you need to use a PropertyDrawer instead. This tutorial on catlikecoding is a pretty quick and easy introduction if you don't like videos, or you can watch this overview on editor scripting (custom editors, property drawers, attribute drawers, etc), or one of the many tutorials on YouTube on the subject.
     
    JG-Denver likes this.