Search Unity

Instantiating object into serialized property.

Discussion in 'Scripting' started by Nemox, Feb 14, 2015.

  1. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    I'd asked this on Answers, but I feel it may require more in-depth discussion. I'll try to describe the situation as simply as possible.

    I have a ScriptableObject, Foo. Foo has a list of custom objects of Bar class. For this project, I have to rely on inheritance for the Bar class with three derived classes, Crobar, Cinnabar, and Rebar.

    In my custom editor script, I have a list of types (the three derived classes), which when selected, should add a new instance of that type to Foo's list of Bars. When I do this, it shows up as None (Bar) in the inspector, but the debug lines I've written show that the serialized Bar != null. It is not null, yet none of the variables are accessible in any way, and even referring to Bar results in a null reference exception.

    So my question is this. How do I add a new instance of a derived Bar class into Foo's serialized list?