How can I add prefabs in a folder to my GameObject array?

Hey guys,

So I wanted to create a system for my items, where it loads all the prefabs from the folder into a generated array list. I can’t seem to get it to work.

using UnityEngine;
using System.Collections;

public class Items : MonoBehaviour 
{
	public enum ItemType
	{
		Potion,
		Sword,
		Quest_Item,
		Shoes
	};
	public enum Effects
	{
		Health,
		Mana,
	};
	public ItemType _itemType;
	public Effects _effects;
	public string ItemName= "";
	public string ItemDescription = "";

	public GameObject[] _items;

	void Start()
	{
		_items = Resources.LoadAll<GameObject> ("Prefabs/Items");
	}
}

Any help would be appreciated :slight_smile:

Is your "Prefabs/Items" folder located in the Resources foulder? Because Resources.Load/LoadAll searches in the Resources folder. Otherwise try _items = (GameObject[]) Resources.LoadAll ("Prefabs/Items"); // cast to GameObject ref.

1 Answer

1

No, you’ll have to create an folder named Resources and add your Prefabs/Items/ folder to the Resources folder and your code will work.