Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Persistence of object type in scriptable objects

Discussion in 'Scripting' started by beneQ_04, May 30, 2023.

  1. beneQ_04

    beneQ_04

    Joined:
    Mar 1, 2020
    Posts:
    21
    Let's assume I have a BaseClass and a DerivedClass that inherits from BaseClass.

    Now if I have a list of BaseClass in my scriptable object
    Code (CSharp):
    1. List<BaseClass> list = new List<BaseClass>()
    and I want to save both instances of BaseClass and DerivedClass into this list
    Code (CSharp):
    1. list.Add(new BaseClass());
    2. list.Add(new DerivedClass());
    will the scriptable object after realoding of unity load the instances into proper types as I saved them or will it load them both as the BaseClass
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,969
    (if incorrect someone else please respond, bit rusty on this)
    By default it will be the base class, since that's the reference type. You can cast to (DerivedClass)list[1] (or as DerivedClass?)
     
  3. Your list will always give back
    BaseClass
    since that's the type you gave to it. So
    list[n]
    's type will always be
    BaseClass
    and you will need to cast it to
    DervicedClass
    when it's appropriate.
     
  4. beneQ_04

    beneQ_04

    Joined:
    Mar 1, 2020
    Posts:
    21
    Well yes it will always give me an object of type BaseClass however if i do object.GetType().Name it will return the name of the correct Class until I reload unity (reopen it or go into play mode)

    My question being: Is there a way to have it save the correct type even after closing unity without directly saving the type and then casting the variable?
     
  5. I'm not sure what you're asking.

    But here is roughly what's happening:

    - you create a list of
    BaseClass
    types
    - you add object reference pointers to this list
    - when you address an element in the array you get back the pointed object regardless of what actual type it is but the resulting variable will be
    BaseClass
    since the list will return
    BaseClass


    When you add the
    DerivedClass
    to a list of
    BaseClass
    , you're telling C# that you want to refer to this object as
    BaseClass
    , nothing more. If you want to use it as
    DerivedClass
    by default, you need a list of
    DerivedClass
    so you get back that type when you reference a member of that list.

    I'm not sure what you're saving, reloading or whatever. Can you expand on this?
     
  6. beneQ_04

    beneQ_04

    Joined:
    Mar 1, 2020
    Posts:
    21
    What i want to be able to do is to have a list in my scriptable object that contains objects that all derive from a base Class

    I want to be albe to make any class that derives from base class and expands the functionality of it

    Finally i want to save the instances of the classes in this one list and retaind their values after loading them back in
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    You need polymorphic serialisation which is done using the [SerializeReference] attribute. Though Unity doesn't have a means of selecting derived types, so you will need to make custom editor tools for this, or use an existing tool that supports this like Odin Inspector.
     
  8. beneQ_04

    beneQ_04

    Joined:
    Mar 1, 2020
    Posts:
    21
    This is exactly what I needed! Thank you!
     
    spiney199 likes this.