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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem while loading in Resources folder with a duplicated content

Discussion in 'Scripting' started by Eidern, Dec 3, 2021.

  1. Eidern

    Eidern

    Joined:
    Mar 29, 2014
    Posts:
    94
    Hi, I've encountered a weird behavior, I don't know if it's a bug, anyway, I made a workaround but I still need to see with everyone if the first code was okay.

    I've made two custom classes, "item" and an inherited one called "Equipment"
    for both I can create directly the an equipment or item asset by right clicking onto the Project directory editor with the line
    [CreateAssetMenu(fileName = "New Equipment", menuName ="Inventory/Equipment")]

    these equipment files are stored in
    Assets/Resources/items

    now, I needed to list all the equipment into a list (fullItemList) in the rescue folder, with the following code :

    Code (CSharp):
    1.   foreach (Equipment itm in Resources.FindObjectsOfTypeAll(typeof(Equipment)))
    2.         {
    3.             fullItemList.Add(itm);
    4.         }
    Everything works fine, if I created let's say 10 equipment with the help of the context menu, if I call a fullItemList.Count I get 10.

    BUT, if I make the mistake to duplicate directly (by Ctrl-D) an equipment asset in this very same folder, there everything breaks, and a fullItemList.Count could gave me a lowered number (like 3), depending on which equipment name was duplicated.
    WORSE : even after deleting the duplicated asset and restarting unity, the list.count will stay the same (lower number version)
    I have to delete all the assets after the duplicated one, and recreate all by context-menu to get everything in order.

    So to counter this, I now use

    Code (CSharp):
    1.  string myPath = "Assets/Resources/items/";
    2.         DirectoryInfo dir = new DirectoryInfo(myPath);
    3.         FileInfo[] info = dir.GetFiles("*.asset");
    4.         foreach (FileInfo f in info)
    5.         {
    6.             Debug.Log(f.Name);
    7.             fullItemList.Add((Equipment)AssetDatabase.LoadAssetAtPath(myPath + f.Name, typeof(Equipment)));
    8.         }
    which works fine, even after duplicate my assets..

    Look like a bug, or did I do something terribly wrong?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,725
    What type of collection is
    fullItemList
    ?
     
  3. Eidern

    Eidern

    Joined:
    Mar 29, 2014
    Posts:
    94
    it's a List<Equipment> fullItemList;

    really everything's work fine, if I keep adding equipments assets by right clicking, the list will increase, but when I duplicate an equipment (and then changing the name and vars in it), there it goes nuts