How do I make my obstacles (separate prefabs) appear randomly in the world?

I am using Unity in C# to code a game similar to Google's Dinosaur Run where random obstacles are spawned in the player's path which they need to jump to dodge. I have 6 different prefabs (with animations) for my obstacles which use Polygon Colliders to detect collisions. How can I use an array to randomly select one of these obstacle prefabs to spawn them at various distances apart?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObstacleController : MonoBehaviour
{
[SerializeField]
private Sprite[] sprites;

public int Type { get; set; }

private SpriteRenderer spriteRenderer;

private PolygonCollider2D polygonCollider2D;

public void Initialise()
{
spriteRenderer = GetComponent<SpriteRenderer>();
polygonCollider2D = GetComponent<PolygonCollider2D>();

int type = Random.Range(0, sprites.Length);

Type = type;
spriteRenderer.sprite = sprites[type];

}
}


Above is the code I have so far which only allows me to have sprites as obstacles whereas I want to use prefabs with set dimensions and animation properties.

• You can use GameObject[] instead of Sprite[] to hold your prefabs Aug 16, 2021 at 12:02

[Serializefield] GameObject [] prefabs;