Search Unity

Even when 'T' is definite, ScriptableObject.CreateInstance<T>() returns null

Discussion in 'Scripting' started by alex_dossamer, Nov 15, 2018.

  1. alex_dossamer

    alex_dossamer

    Joined:
    Jun 19, 2018
    Posts:
    25
    So I'm aware that for ScriptableObject.CreateInstance<T> to work, 'T' can't remain generic.
    I have the following code, in which 'T' is eventually made definite:

    Abstract base class
    Code (CSharp):
    1.  
    2.     public abstract class MouthSetGenericEditor<T> : Editor
    3.         {
    4.             public static void CreateMouthSet(T fillerResource)
    5.             {
    6.                 var mouthset = ScriptableObjectUtility.CreateAsset<MouthSetGeneric<T>>();
    7.             }
    8.         }
    9.  
    Child class where type is made definite
    Code (CSharp):
    1.  
    2.     [CustomEditor(typeof(MouthSetGeneric<Sprite>))]
    3.         public class MouthSetSpriteEditor : MouthSetGenericEditor<Sprite>
    4.         {
    5.             private static readonly Sprite placeholderSprite = Resources.Load<Sprite>("Placeholder");
    6.    
    7.             [MenuItem("Assets/Rhubarb/Create Sprite Mouthset")]
    8.             public static void CreateSpriteMouthSet()
    9.             {
    10.                 CreateMouthSet(placeholderSprite);
    11.             }
    12.         }
    13.  
    Utility where ScriptableObject.CreateInstance<T>() is called
    Code (CSharp):
    1.  
    2.     public static class ScriptableObjectUtility
    3.         {
    4.             public static T CreateAsset<T>() where T : ScriptableObject
    5.             {
    6.                 T asset = ScriptableObject.CreateInstance<T>();
    7.    
    8.                 if (asset == null)
    9.                     Debug.Log("asset null");
    10.    
    11.                 // more code...
    12.              }
    13.         }
    14.  
    In the same block where ScriptableObject.CreateInstance<T>() is called, if I log typeof(T).ToString(), the console outputs MouthSetGeneric`1[UnityEngine.Sprite]. So it looks like type T was made definite, but the ScriptableObject instantiation call isn't behaving as though it is.

    Any idea what could be going on? Thanks!