Hello, im making moving and randomly spawning background for my game. For now i got 3 different prefabs as background, i want to spawn randomly 1 of them every 2secs or smth. But i got error that Argument is out of range. Prameter name: index. Here’s my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BackGround : MonoBehaviour {
List<GameObject> bgList = new List<GameObject>();
public GameObject bg1;
public GameObject bg2;
public GameObject bg3;
void Start(){
StartCoroutine(bgSpawn());
bgList.Add(bg1);
bgList.Add(bg2);
bgList.Add(bg3);
}
IEnumerator bgSpawn(){
while (true) {
int bgIndex = Random.Range(0, bgList.Count);
Instantiate(bgList[bgIndex], transform.position, transform.rotation);
yield return new WaitForSeconds(2.482f);
}
}
}