- Home /
How to create .asset file?
--WHAT I WANT TO DO--
I'm trying to load data from a JSON file into a ScriptableObject. When my Database editor window loads it checks if there is a database.asset file, and if so loads the data but if not, it will create the .asset file and then load the data.
--PROBLEM--
Asset.Database.CreateAsset(obj, "path") isn't working. I've had a look online and as far as I can see, I'm doing things correctly... However issue seems to be with the path. Errors I get are;
Invalid AssetDatabase path:
F:/Unity/UnityDatabase/Assets/scriptableObjects/rpg_Database.asset. Use path relative to the project
folderCouldn't create asset file!
UnityException: Creating asset at path
F:/Unity/UnityDatabase/Assets/scriptableObjects/rpg_Database.asset failed.
--CODE--
static void Open()
{
rpg_Database db;
if (File.Exists(Application.dataPath + "/scriptableObjects/rpg_Database.asset"))
{
//Load Database from JSON
Debug.Log("Database Asset Found!");
}
else
{
db = ScriptableObject.CreateInstance("rpg_Database") as rpg_Database;
AssetDatabase.CreateAsset(db, Application.dataPath + "/scriptableObjects/rpg_Database.asset");
Debug.Log("Database Asset Created!");
//Load Database from JSON
}
}
Uhm you closed the question without any reason. Is the question outdated? If so you may want to delete the question because others would just find the question and get no answer.
About your problem: You used an absolute path because you included the Application.dataPath property. An Asset path in Unity is simply relative to the project root folder. So an Asset path should start with the "Assets" folder. You can see an example how to use it in the code example on the CreateAsset documentation page.