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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to edit an array of objects in a ScriptableObject

Discussion in 'Editor & General Support' started by wmcritter, Feb 28, 2018.

  1. wmcritter

    wmcritter

    Joined:
    Feb 15, 2015
    Posts:
    8
    I'm trying to edit an array of objects in a ScriptableObject, and I can't quite get it. I've tried making an Editor and a PropertyDrawer, but neither one quite works. Pseudo code is below.

    Code (CSharp):
    1. public class MyScriptableObject : ScriptableObject
    2. {
    3.   public string Name;
    4.   public int ANumber;
    5.  
    6.   public DataObject[] Data;
    7. }
    8.  
    9. [System.Serializable]
    10. public class DataObject
    11. {
    12.   public string MyProperty1;
    13.   public string MyProperty2;
    14. }
    The editor lets me edit Name and ANumber just fine. And it shows Data as an array, to which I can add and remove objects. But, inside the MyScriptableObject editor, I can't get the DataObjects in the Data array to show an editor for MyProperty1 and MyProperty2.
    Is this possible, and if so, how can it be done?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just to be clear.. you really want to make an editor/property drawer, or were you just trying that to get them to show up? I just ask because this code does show the two properties in the inspector, normally.
     
  3. wmcritter

    wmcritter

    Joined:
    Feb 15, 2015
    Posts:
    8
    For some reason, my array does not show correctly. I've included a screenshot. Capture.PNG
     
  4. wmcritter

    wmcritter

    Joined:
    Feb 15, 2015
    Posts:
    8
    Oops, I just figured it out. My data object populating the array was inheriting from MonoBehavior. Sometimes it's convenient that Unity inherits from MonoBehavior by default, and sometimes not so much.
     
  5. seandanger

    seandanger

    Joined:
    Jun 13, 2012
    Posts:
    39
    Another gotcha is to make sure that the class of whatever collection you'd like to see in the Editor is marked as Serializable. Like so:

    Code (CSharp):
    1. [Serializable] // <---- this is required!
    2. public class MyFancyClass
    3. {
    4.     public string name;
    5.     public int id;
    6. }