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;
}