Customer Editor: What should I set this variable to?

Hi all,

I know this may seem like a pathetic question but this is my first go with custom editors. My question is what should I set the variable (var myScript) to?

As you can see in the code, I have to use “var myScript = target as ScriptableItem;” rather often and I want this to be set at the begining of the script so I can reference it. However if i move this to the top I get the error: The contextual keyword ‘var’ may only appear within a local variable declaration or in script code

[CustomEditor(typeof(ScriptableItem), true)]
public class ScriptableItemEditor : Editor
{
    string[] buttons = new string[] { "Item Details", "Tradeability", "Graphics" };
    public override void OnInspectorGUI()
    {
        ScriptableItemGUI();
    }

    public void ScriptableItemGUI()
    {
        var myScript = target as ScriptableItem;
        EditorGUILayout.LabelField("Item Characteristics", EditorStyles.boldLabel);
        myScript.currentTabItem = GUILayout.Toolbar(myScript.currentTabItem, buttons);
        EditorGUILayout.Space();
        switch (myScript.currentTabItem)
        {
            case 0:
                ItemDetailsSection();
                break;
            case 1:
                TradeabilitySection();
                break;
            case 2:
                GraphicsSection();
                break;
        }
    }
    public virtual void ItemDetailsSection()
    {
        var myScript = target as ScriptableItem;

// Variables go here
    }
    public virtual void TradeabilitySection()
    {
        var myScript = target as ScriptableItem;


// Variables go here
        }
    }
    public virtual void GraphicsSection()
    {
        var myScript = target as ScriptableItem;

// Variables go here
    }
}


public class ScriptableItem : ScriptableObject
{
    // Editor Specific
    public int currentTabItem;
    // Item Details
    public string itemName;
    public string itemDescription;
    public float itemWeight;
    public int maxInventoryStack;
    protected string toolTip;
    // Tradeability
    public bool tradeable;
    public long buyPrice;
    public long sellPrice;
    // Graphics
    public GameObject droppedModel;
    public Sprite icon = null;
}

I’m not sure I understand the question. Are you trying to make individual editors for each child class? Or avoid making individual editor classes for each child class? Or something else entirely? You’re setting myScript to be a ScriptableItem. Is this wrong? What are you missing?

I updated my post, hopefully that makes it more clear.

Ah, I see. The var keyword isn’t allowed when declaring fields, so you’ll just need to explicitly provide the type:

ScriptableItem myScript;

You’ll also want to set the value of myScript in OnEnable:

void OnEnable () {
  myScript = target as ScriptableItem;
}

My go-to pattern is to declare a field that hides the underlying field via the new keyword:

new ScriptableItem target;

void OnEnable () {
  target = base.target as ScriptableItem;
}

This allows me to use target transparently as if I’d never overridden it at all, but still get the benefit of having a strongly typed variable.

1 Like

Amazing, it works perfectly! I cant believe I completely overlooked this method. Thank you so much for your help, I really appreciate your time!