I am very much a novice to coding with no background on this at all so the code you see here may be terrible.
Basically I’m making an endless runner, made up of a player character and a set of obstacle prefabs. The obstacle prefabs have a trigger on them so that when the player hits it, it spawns the next prefab. I’m using the code below, and when the player hits the first spawn trigger it will spawn a prefab, but on tripping the next trigger it gives me the ArgumentOutOfRangeException error in the console and doesn’t spawn anything.
public class ObsTrigger : MonoBehaviour {
public GameObject spawnTrigger;
[SerializeField] private List<Transform> obstacleList;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
for (int i = 0; i < obstacleList.Count; i++)
{
spawnObstacle();
}
}
}
public void spawnObstacle()
{
{
Transform chosenLevelPart = obstacleList[Random.Range(0, obstacleList.Count -1)];
Instantiate(chosenLevelPart);
}
}
}
If anyone can point me in the right direction I would really appreciate it!