Hi, i’m kinda new with Unity.
So I have a “Loot” object in my scene. I want it to contain some random items. My items are all ScriptableObject like this:
public class Item : ScriptableObject {
new public string name;
public Sprite icon;
public string type;
public int rarity;
public virtual void Use() {
//Use Item
}
}
Is there a way to pick a random Item from my Assets Folder?
Yes there is! It’s pretty simple. Steps:
-
use Resources.LoadAll<Item>( "PathToMyItemsAssetsFolder/");
to get them all
-
shuffle that list and iterate through, OR just pick randomly (that’s a game design decision up to you)
Warning: be sure to put those items in their own folder. The reason: if you do a Resources.LoadAll<Item>("SomeCommonFolderOrTheRootFolder/")
, Unity will load every single asset into memory one at a time, and only after loading will it check if each one is an Item
or not, before only returning the list of Items. This may crash your game if you have a lot of assets in your Resources folder.
It’s not working, idk why.
Here are the items in my assets:

I’m trying to print the length of the array to see if it’s filling:
Item[] allItems = Resources.LoadAll<Item>("Items/");
Debug.Log(allItems.Length);
But it prints 0

Anything and everything you want Resources to see MUST be located somewhere under a folder with the name Resources
.