Right now, to import a sprite (Texture 2D asset), I use Resources.LoadAll(“path/to/resource”). That works just fine, but I’ve noticed something odd, and it’s got me wondering if I should be using some other technique.
The problem:
// This file is a spritesheet of all of my enemies (and their respective alternate sprites for animations) in the game
List<Sprite> enemySprites = Resources.LoadAll<Sprite>("path/to/enemySprites").ToList();
// I figured ToList() would make a clone of this list, but I guess not a deep clone... I've also tried Select, but no dice
List<Sprite> enemy1 = enemySprites.Where(x => x.name.contains(enemyName)).ToList();
foreach (Sprite sprite in enemy1) {
// This actually overwrites the sprite's name in the Resources.LoadAll call if I try to access it again
sprite.name = sprite.name.Replace(enemyName, "");
}
The code above works on the first time, but any subsequent time has an empty list for enemy1 (because the name has been overwritten in the foreach). This happens even if I stop play mode and enter back into it. I believe it has something to do with how resources are cached (and how in my actual game, I’ve got this tied to a static property), but I’ve searched all over, and I can’t find how to force clear the cached resources… tried GC.Collect, AssetDatabase.Refresh, and other commands that I’ve forgotten now. The only way to get this to go back to working for the first time is closing Unity Editor altogether and reopening it.
Having said all of that, should I not be loading sprites this way, and instead maybe try to make them a material, and apply it to a mesh? I’m fairly new to all of this, so I’d appreciate any guidance, and I apologize if this is a dupe thread… my searching failed me.