ScriptableObject Help! What am I missing here?

I have the following data container.

[System.Serializable]
public class SomeDataType
{
    public int Value;
}

With the following object holding a list of those objects.

public class DataTypeHolder : ScriptableObject
{
    public List<SomeDataType> ListOfData;
}

Then I try to create a menu item to create the object

using UnityEngine;
using UnityEditor;
using System.Collections;

public class DataTypeCreator
{
    [MenuItem("Assets/Create/Some Data Type")]
    public static void CreateAsset()
    {
        ScriptableObjectUtility.CreateAsset<DataTypeHolder>();
    }	
}

and lastly the ScriptableObjectUtility

using UnityEngine;
using UnityEditor;
using System.IO;


public static class ScriptableObjectUtility
{
    public static void CreateAsset<T>() where T : ScriptableObject
    {
        T asset = ScriptableObject.CreateInstance<T>();

        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        if (path == "")
        {
            path = "Assets";
        }
        else if (Path.GetExtension(path) != "")
        {
            path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
        }

        string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/New " + typeof(T).ToString() + ".asset");

        AssetDatabase.CreateAsset(asset, assetPathAndName);

        AssetDatabase.SaveAssets();
        EditorUtility.FocusProjectWindow();
        Selection.activeObject = asset;
    }
}

Thanks in advance for helping point me in the right direction :slight_smile:

Edit: I must be too tired … I forgot to post the error I am getting …

Instance of DataTypeHolder couldn’t be created because there is no script with that name.
UnityEngine.ScriptableObject:CreateInstance()

It’s really strange. Is the file, containing DataTypeHolder class also named “DataTypeHolder.cs” (it should be)?

Here is the code from my project to do the same thing. It works fine in both Unity 3.5 and 4. PPAudioClipAlias is a ScriptableObject.

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Linq;
using System.IO;

public static class SoundAliasTools
{
        [MenuItem("Assets/Create/Audio Clip Alias")]
        public static void CreateAlias(MenuCommand command)
        {
                string folder = null;
                if (Selection.objects == null || Selection.objects.Length == 0)
                {
                        folder = "Assets";
                }
                else if (Selection.objects.Length == 1  Selection.objects[0] != null)
                {
                        var p = AssetDatabase.GetAssetPath(Selection.objects[0]);
                        if (!string.IsNullOrEmpty(p)  Directory.Exists(p))
                        {
                                folder = p;
                        }
                }

                if (folder != null)
                {
                        const string nameBase = "AudioClipAlias";
                        int i = 1;
                        var name = Path.Combine(folder, nameBase + ".asset");
                        while (File.Exists(name) || Directory.Exists(name))
                        {
                                name = Path.Combine(folder, nameBase + (i++) + ".asset");
                        }

                        var data = ScriptableObject.CreateInstance<PPAudioClipAlias>();
                        AssetDatabase.CreateAsset(data, name);
                        return;
                }


                foreach (var o in Selection.objects)
                {
                        var clip = o as AudioClip;
                        if (clip == null)
                        {
                                continue;
                        }

                        folder = Path.GetDirectoryName(AssetDatabase.GetAssetPath(clip));
                        string nameBase = clip.name + "_Alias";
                        int i = 1;
                        var name = Path.Combine(folder, nameBase + ".asset");
                        while (File.Exists(name) || Directory.Exists(name))
                        {
                                name = Path.Combine(folder, nameBase + (i++) + ".asset");
                        }
                        
                        var data = ScriptableObject.CreateInstance<PPAudioClipAlias>();
                        data.Sound = clip;
                        AssetDatabase.CreateAsset(data, name);
                }
        }
}

Right before I fell asleep last night, I realized that is what I had done. The file holding the DataType was not named DataTypeHolder.cs. It was named after the DataType which was also in the file with the DataTypeHolder.

Thank you for the reply :slight_smile: