How to Properly pass DefaultAsset type objects into a field of matching type ?

lets say you have a folder named “Cat Songs” in you local path is folder “Assets/Cat Game For Kids/Cat Songs”

your full path to the folder is

“c:/ MyGameProjects/Unity/projects/Cat Game/Assets/Cat Game For Kids/Cat Songs”

we know that folders in the unity project are of type DefaultAsset.

so now we have a script like this which inherits from ScriptableObject and for the sake of a shorter code we say that this ScriptableObject has been created and now lived in our project folder.

using system.IO;
public class SomeScript: ScriptableObject
{
public  DefaultAsset folder;
public void OnEnable()
{
string path = "c:/ MyGameProjects/Unity/projects/Cat Game/Assets/Cat Game For Kids/Cat Songs"


//folder = ???

// we cant use  , folder = AssetDatabase.LoadAssetAtPath(path+".DefaultAsset",typeof(DefaultAsset)) as DefaultAsset;

}

}

As you can see manually dragging a folder into the “folder” field in the editor works fine.
but how do we do this in c#

I just tested this code, it appears to work:

var o = AssetDatabase.LoadAssetAtPath<DefaultAsset>("Assets/Editor");

This returns the DefaultAsset for the Assets/Editor folder. You can do the same for any other folder you like.

1 Like