So, I would like to, when I collide with an object, instantiate a prefab from a list. And I would like the prefab to be Randomly picked from a specific place. I was thinking an array would work, but I have no idea how to make those. I have looked everywhere and I cant seem to find an answer so i’m putting it here. Thanks for your help.
Yes an array works well.
In a C# script you could declare an array of GameObjects, here I’m calling them buildings:
public GameObject[] buildings;
Then in the Unity Editor you can change the length of this array and select or drag and drop Prefabs from your assets folder.
You can create a getBuilding() function that returns a random prefab from your array:
private GameObject getBuilding() {
return buildings[Random.Range(0, buildings.Length - 1)];
}
Then instantiate the randomly selected prefab:
Vector3 buildingPos = new Vector3(100, 100, 100);
buildingPrefab = getBuilding();GameObject spawnedBuilding = Instantiate(buildingPrefab, buildingPos, buildingPrefab.transform.rotation) as GameObject;