- Home /
Error from declaring List within ScriptableObject
My scriptable object:
[CreateAssetMenu(fileName = "AudioCollection", menuName = "AudioCollection")]
public class AudioClipCollection : ScriptableObject {
[System.Serializable]
public class Clip{
public string name;
public AudioClip audioClip;
}
public List<Clip> clips = new List<Clip>();
}
The list declaration line gives the error:
AudioClipCollection must be instantiated using the ScriptableObject.CreateInstance method
instead of new AudioClipCollection.
How am I meant to be declaring lists within a scriptable object? I have used 'ScriptableObject.CreateInstance' to create the scriptable object, so that is not the issue.
Cheers!
Answer by Adam-Mechtley · Feb 12, 2017 at 11:15 AM
Your error doesn't look like it is related to to your List initialization. Rather, you are probably doing this somewhere var collection = new AudioClipCollection()
where you need to be doing this: var collection = ScriptableObject.CreateInstance<AudioClipCollection>()
+1, but be aware that ScriptableObjects simply created through CreateInstance will be destroyed on load, and must be ACTUALLY stored in the assets folder through the AssetDatabase. As such, it is best practice to create a custom EditorWindow for creating your ScriptableObjects, at least some sort of In-Editor method of creating instances as .asset files.