im going to be spawning a lot of different prefabs so I want to be able to just drop them in the folder
i just learned how to instantiate from the resource folder
var instance : GameObject = Instantiate(Resources.Load("prefab name"));
now I’m just trying to figure out how I can instantiate random objects from the folder.
Is it even possible to instantiate without the name of the object?
thanks
Put the prefabs in a sub folder of resources called “Prefabs” then
var prefabs : Object[] = Resources.LoadAll("Prefabs");
function SpawnRandom() : GameObject
{
var toSpawn : GameObject = prefabs[Random.Range(0, prefabs.Length)];
var spawned = Instantiate(toSpawn);
return spawned;
}
If you named them with a number like, “prefab0”, “prefab1”,…“prefab24”, you could do something like:
var name = "prefab"+Random.Range(0, 25);
Then you can instantiate using the generated name. Note Random.Range() of integers is exclusive (does not include) the last value.
If don’t want to name them with an order, you can build an array and assign the strings in the inspector.
Note this method will result in duplicates. That is each time you instantiate you will have an equal chance of selecting any string including any that have already been selected.