Scriptable object asset not saving

Hi. I created a method to save objects that inherit from ScriptableObject. It’s creating the new asset just fine but its not saving the inherited data when the asset is created. I know I’m just not understanding something crucial here.

	public static void CreateAssetAtPath<T> (string path, string AssetName,T asset) where T : ScriptableObject
	{
		//T asset = ScriptableObject.CreateInstance<T>();

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

		AssetDatabase.CreateAsset(asset,assetPathAndName);
		AssetDatabase.SaveAssets();
	}

and here is the asset I’m trying to save after its filled out by another script.

[System.Serializable]
public class SwipeSession : ScriptableObject {

	public string name = "";
	public List<Vector3[]> levels = new List<Vector3[]>();
}
// Serializable attribute is not required, as ScriptableObject is already flagged.
    public class SwipeSession : ScriptableObject 
    {
        // name is a field of UnityEngine.Object. You should probably flag it "new" or ignore it.
        // UnityEngine.Object name field also is special as it automatically take the file's name, but won't change the file's name if modified.
        //public string name = "";
        public List<Vector3[]> levels = new List<Vector3[]>();
    }

Beside that, you’re not showing us how you create the asset (ScriptableObject.CreateInstance) and how you fill its data because saving it.

thanks lightStriker.

I renamed the name field to session name. The name is being saved correctly now (I can see it).

I’m declaring the swipeSession like this.

SwipeSession session = ScriptableObject.CreateInstance<SwipeSession>();
session.sessionName = testString;

Code that I’m using to fill in the data is below.

	private void saveAndWriteSession()
	{
		saveLevel();

		CustomAssetUtility.CreateAssetAtPath("SwipeSessions",session.sessionName,session);
	}

	private void saveLevel()
	{

		Vector3[] lvlData = new Vector3[record.Count];

		for(int i = 0; i < lvlData.Length;i++)
			lvlData[i] = findTriangleCenter(record.Pop());

		session.levels.Add(lvlData);
		clear();
	}

I’m using this to test it

public class CustomAssetTester : MonoBehaviour {

	public SwipeSession session;
	// Use this for initialization
	void Start () 
	{
		Debug.Log(session.sessionName);
		Debug.Log("Session Levels:" + session.levels.Count);
	}
}

Session name is being outputted correctly but Session levels is giving me zero. Not sure how one can work but not the other.

What “clear()” does?

clear() just clears my stacks. like record.

But I found the problem. This way of saving data will ONLY save the data if you can see it through unity’s inspector. I tried saving a single Vector[ ] and that worked but the moment i turned it into a Vector[ ][ ] it broke.

So I need a new way to save and load this data…

Yeah, Unity doesn’t support nested list of any kind. ANY kind… I tried them all.

What you’ll have to do, is a wrapper class around a list… And a list of that wrapper.

[Serializable]
public class Wrapper
{
    public List<Vector3> list;
}

public class Container : ScriptableObject
{
    public List<Wrapper> list;
}

Not as clean as nested list, but sadly the only way.

woah…ok well that’ll work. What a work around…lol. Thanks for the help LightStriker.