How do you make an array out of an Assets subfolder containing Prefabs?

This is in C#. I want to make an array and select random character prefabs forexample to instantiate into my scene at Spawn points.

While it’s not possible during runtime, you could whip up a small Editor script to parse your directory and set the prefabs into an array for you. This is really dirty code and could use some cleaning up (like getting rid of warnings), but it does the job:

First, your prefab manager:

using UnityEngine;
public class PrefabManager : MonoBehaviour
{
    [SerializeField]
    string prefabDirectory;

    [SerializeField]
    GameObject[] prefabs;

    public GameObject GetRandomPrefab()
    {
        return prefabs[Random.Range(0, prefabs.Length - 1)];
    }
}

Then your Editor script:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PrefabManager))]
public class PrefabManagerEditor : Editor
{
    Vector2 scrollPosition;
    public override void OnInspectorGUI()
    {
        SerializedProperty prefabDirectory = serializedObject.FindProperty("prefabDirectory");
        EditorGUILayout.PropertyField(prefabDirectory);

        // find prefabs in that directory and list them up
        string[] assetGUIDs = AssetDatabase.FindAssets("t:prefab", new string[] { prefabDirectory.stringValue });

        EditorGUILayout.LabelField("Assets found:");
        scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
        if (assetGUIDs != null)
        {
            SerializedProperty prefabArray = serializedObject.FindProperty("prefabs");
            prefabArray.arraySize = assetGUIDs.Length;
            for (int i = 0; i < assetGUIDs.Length; i++)
            {
                string assetPath = AssetDatabase.GUIDToAssetPath(assetGUIDs*);*

EditorGUILayout.LabelField(assetPath);
prefabArray.GetArrayElementAtIndex(i).objectReferenceValue = AssetDatabase.LoadAssetAtPath(assetPath);
}
}
EditorGUILayout.EndScrollView();

serializedObject.ApplyModifiedProperties();
}
}

“While it’s not possible during runtime,” Interesting i thought it was possible to instantiate objects during runtime. alot of tutorials have it. Like they’ll make grenade or bullet prefabs and then instantiate during runtime. So why wouldn’t it be possible for arrays and folders with prefabs inside?