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. Dismiss Notice

Scriptable Objects with serialized generic type

Discussion in 'Scripting' started by DarylWinters, Aug 24, 2020.

  1. DarylWinters

    DarylWinters

    Joined:
    Jan 23, 2019
    Posts:
    5
    Hello,
    Since with Unity 2020.1 we're now able to serialize generic types, I was wondering if its possible to make the
    Code (CSharp):
    1. ScriptableObject.CreateInstance<GenericScriptableObject<...>()>
    works too in order to not to have to implement a non-generic class before.

    For exemple I have,
    Code (CSharp):
    1. public class Variable<T> : ScriptableObject
    2. {
    3.     private T _value = default;
    4.     public T Value
    5.     {
    6.         get => _value;
    7.         set => SetValue(value);
    8.     }
    9.  
    10.     public void SetValue(T value)
    11.     {
    12.         _value = value;
    13.     }
    14.  
    15. }
    and I want to be able to do
    Code (CSharp):
    1.  
    2.     public Variable<float> floatVariable;
    3.     void Start()
    4.     {
    5.         floatVariable = ScriptableObject.CreateInstance<Variable<float>>();
    6.     }
    (For now ScriptableObject.CreateInstance<Variable<float>>() just returns null)
    Thanks !
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Since it is brand-new capability, it seems like you might be the person best positioned to try it out, do some testing, evaluate it against your own specific criteria of "works for me!" and report back here if you like. I've never messed with that stuff myself.
     
    GilbertoBitt likes this.
  3. DarylWinters

    DarylWinters

    Joined:
    Jan 23, 2019
    Posts:
    5
    Sadly it does'nt work, I still need to inherit in a non generic class before being able to create a scriptable object :c