Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Can't create asset with dynamic(valid) path

Discussion in 'Editor & General Support' started by matet2001, Jun 20, 2023.

  1. matet2001

    matet2001

    Joined:
    Sep 28, 2021
    Posts:
    1
    Code (CSharp):
    1. [Button]
    2.     private void CreateItemTypes()
    3.     {
    4.         Item itemType = ScriptableObject.CreateInstance<Item>();
    5.         itemType.name = itemNames[1];
    6.         itemType.sprite = itemSprites[1];
    7.         itemType.type = (ItemData.Types)1;
    8.  
    9.         string path = "Assets/CozyTown/Temp/Tomato.asset";
    10.         Debug.Log("Try to create item on: " + path);
    11.         AssetDatabase.CreateAsset(itemType, path);
    12.  
    13.         itemType = ScriptableObject.CreateInstance<Item>();
    14.         itemType.name = itemNames[0];
    15.         itemType.sprite = itemSprites[0];
    16.         itemType.type = (ItemData.Types)0;
    17.  
    18.         path = $"Assets/CozyTown/Temp/{itemNames[0]}.asset";
    19.         Debug.Log("Try to create item on: " + path);
    20.         AssetDatabase.CreateAsset(itemType, path);
    21.  
    22.         AssetDatabase.SaveAssets();
    23.         AssetDatabase.Refresh();
    24.         EditorUtility.FocusProjectWindow();
    25.     }
    I want to create a script, which create a scribtable object asset, for every item type, with the correct sprite, name etc..
    The first block, which has hard coded path name work properly. The second, which is technacly the same, but with other name, is for some reason, dropping the following error :

    Failed to move file from "Temp/assetCreatePath" to "Assets/CozyTown/Temp/None
    .asset"
    UnityException: Creating asset at path Assets/CozyTown/Temp/None
    .asset failed.

    And my logs:
    Try to create item on: Assets/CozyTown/Temp/None
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Does a None.asset already exist at that location? If so, that will fail.

    Also note that you shouldn't be calling AssetDatabase.Refresh() here. This is ONLY needed when you create/change/remove files or folders WITHOUT AssetDatabase methods. Since you save the asset through the asset database it is not required and in fact adds extra processing overhead to running this script.

    Calling Refresh() here is basically the same as creating any asset type through the asset menu, and then pressing Ctrl+R to refresh the Asset Database. When you look at it this way, I hope it's clear that this is unnecessary. ;)
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Based on this code:

    Then it is extremely suspicious you get this log:

    Why?

    Because where is the
    .asset
    portion of it??

    I think you aren't running the code you think you are, or else you have "None\n" with a line ending after it.

    Whenever you print filenames like that it can be helpful to print it between something, like so:

    Code (csharp):
    1. Debug.Log( "[" + path + "]");
    and if you don't see that closing brace you got a problem.