Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Customer Editor: What should I set this variable to?

Discussion in 'Scripting' started by Langdelliooo, Nov 2, 2019.

  1. Langdelliooo

    Langdelliooo

    Joined:
    Sep 20, 2018
    Posts:
    17
    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

    Code (CSharp):
    1.  
    2.  
    3. [CustomEditor(typeof(ScriptableItem), true)]
    4. public class ScriptableItemEditor : Editor
    5. {
    6.     string[] buttons = new string[] { "Item Details", "Tradeability", "Graphics" };
    7.     public override void OnInspectorGUI()
    8.     {
    9.         ScriptableItemGUI();
    10.     }
    11.  
    12.     public void ScriptableItemGUI()
    13.     {
    14.         var myScript = target as ScriptableItem;
    15.         EditorGUILayout.LabelField("Item Characteristics", EditorStyles.boldLabel);
    16.         myScript.currentTabItem = GUILayout.Toolbar(myScript.currentTabItem, buttons);
    17.         EditorGUILayout.Space();
    18.         switch (myScript.currentTabItem)
    19.         {
    20.             case 0:
    21.                 ItemDetailsSection();
    22.                 break;
    23.             case 1:
    24.                 TradeabilitySection();
    25.                 break;
    26.             case 2:
    27.                 GraphicsSection();
    28.                 break;
    29.         }
    30.     }
    31.     public virtual void ItemDetailsSection()
    32.     {
    33.         var myScript = target as ScriptableItem;
    34.  
    35. // Variables go here
    36.     }
    37.     public virtual void TradeabilitySection()
    38.     {
    39.         var myScript = target as ScriptableItem;
    40.  
    41.  
    42. // Variables go here
    43.         }
    44.     }
    45.     public virtual void GraphicsSection()
    46.     {
    47.         var myScript = target as ScriptableItem;
    48.  
    49. // Variables go here
    50.     }
    51. }
    52.  
    53.  
    54. public class ScriptableItem : ScriptableObject
    55. {
    56.     // Editor Specific
    57.     public int currentTabItem;
    58.     // Item Details
    59.     public string itemName;
    60.     public string itemDescription;
    61.     public float itemWeight;
    62.     public int maxInventoryStack;
    63.     protected string toolTip;
    64.     // Tradeability
    65.     public bool tradeable;
    66.     public long buyPrice;
    67.     public long sellPrice;
    68.     // Graphics
    69.     public GameObject droppedModel;
    70.     public Sprite icon = null;
    71. }
    72.  
    73.  
    74.  
    75.  
     
    Last edited: Nov 3, 2019
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    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?
     
  3. Langdelliooo

    Langdelliooo

    Joined:
    Sep 20, 2018
    Posts:
    17
    I updated my post, hopefully that makes it more clear.
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Ah, I see. The var keyword isn't allowed when declaring fields, so you'll just need to explicitly provide the type:

    Code (CSharp):
    1. ScriptableItem myScript;
    You'll also want to set the value of
    myScript
    in OnEnable:

    Code (CSharp):
    1. void OnEnable () {
    2.   myScript = target as ScriptableItem;
    3. }
    My go-to pattern is to declare a field that hides the underlying field via the
    new
    keyword:

    Code (CSharp):
    1. new ScriptableItem target;
    2.  
    3. void OnEnable () {
    4.   target = base.target as ScriptableItem;
    5. }
    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.
     
    Langdelliooo likes this.
  5. Langdelliooo

    Langdelliooo

    Joined:
    Sep 20, 2018
    Posts:
    17
    Amazing, it works perfectly! I cant believe I completely overlooked this method. Thank you so much for your help, I really appreciate your time!