Editor Script for Scriptable Object

so i’m working on this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace CoS
{
    [CustomEditor(typeof(ItemHolder))]
    public class InspectorItemHolder : Editor
    {

        ItemHolder itemh;

        void OnEnable()
        {
            itemh = (ItemHolder)target;
        }

        public override void OnInspectorGUI()
        {
            itemh.quantity = EditorGUILayout.IntField("Quantity: ", itemh.quantity);

            EditorGUILayout.Space();

            itemh.item = EditorGUILayout.ObjectField("Item: ", itemh.item);


            if (GUI.changed)
                EditorUtility.SetDirty(itemh);
        }
    }
}

it says ObjectField is deprecated (and my syntax may be wrong) so what do i use instead? and do i have all the bracketed [ ]'s i need?

It’s likely only the specific overload (string, object) that’s deprecated- use the one that includes the specific type in the object picker, and whether to include scene objects in the search (string, object, type, bool) and it should work fine. You may need to cast the result back to the Item class.

1 Like

i’m having trouble with the casting and i did a google search. how do i cast item or itemholder.item to an object?

It should cast to Object automatically I should think, so this isn’t necessary, but for the record it’s like this:

itemh.item = EditorGUILayout.ObjectField("Item: ", (Object)itemh.item);

// ... or...

itemh.item = EditorGUILayout.ObjectField("Item: ", itemh.item as Object);

What I meant was that the ObjectField actually returns an Object, so you likely need to cast it back to whatever your Item class is. Like this:

itemh.item = (Item)EditorGUILayout.ObjectField("Item: ", itemh.item, typeof(Item), true);

… but replace “Item” with whatever your actual item class is called.

1 Like