Spawn from array

Hello, I am trying to make a simple unity game just to learn something :slight_smile:
I want to spawn some obstacles from an array at a given position. How can i do this?
I looked around on google and here but i have not found a simple solution for me. Help is appreciated :slight_smile: I use above all c#.
Thank you!!!

This isn’t a script writing service.

There are a LOT of examples of this all over the forums and UA… people blog about it all the time.
The examples may not be 100% what you want, but you will find components of what you want to do i’m sure.

If you clicked on the spawn tag that you put on this post, you will find tons of examples from random, to percentage based, etc.

The gist of it is:
Have a script with an array or list of type GameObject or Transform. Make it a public field. Set the field length in the inspector. Drag prefabs from your project to the various index locations.

Get a reference to this component in the script that randomizes the spawning gameobject and location/position.

Use Random.range to pick an index from your List (Random.range(0, yourList.Count(or length if using array)).

Instantiate using object from index.

Rinse repeat.

GameObject NewInstance = Instantiate (objArray[Random.Range(0,objArray.Length-1)],
new Vector3 (x, y, z),
Quaternion.identity) as GameObject;
NewInstance.name += β€œβ€œ+x+””+y+β€œ_”+z;