I am currently making a randomized infinite platform with GameObject already made platforms in 3 arrays. In the inspector everything is obviously running quite smoothly but I keep weirdly keep getting uncalled for errors like: IndexOutOfRangeException: Array index is out of range.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackFloorSetter : MonoBehaviour {
public GameObject[] gameBackFloorsOne;
public GameObject[] gameBackFloorsTwo;
public GameObject[] gameBackFloorsFinal;
public Vector3 nextPosition;
public float unroundedFinalOrTwo;
public int finalOrTwo;
public float unroundedTwoFloor;
public int twoFloor;
public float unroundedFinalFloor;
public int finalFloor;
public GameObject currentSpawnFloor;
public float floorSpawnCounter;
public int counterGoal = 5;
void Start()
{
nextPosition = new Vector3(0, 0, 0);
Instantiate(gameBackFloorsOne[0], nextPosition, Quaternion.identity);
}
void Update()
{
floorSpawnCounter += 1 * Time.deltaTime;
if(floorSpawnCounter == counterGoal)
{
floorSpawner();
counterGoal += 5;
}
unroundedFinalOrTwo = Random.Range(0.6f, 2.4f);
finalOrTwo = Mathf.RoundToInt(unroundedFinalOrTwo);
unroundedTwoFloor = Random.Range(-0.6f, 3.4f);
twoFloor = Mathf.RoundToInt(unroundedTwoFloor);
unroundedFinalFloor = Random.Range(-0.6f, 4.4f);
finalFloor = Mathf.RoundToInt(unroundedFinalFloor);
if (finalOrTwo == 1)
{
currentSpawnFloor = gameBackFloorsTwo[twoFloor];
}
if (finalOrTwo == 2)
{
currentSpawnFloor = gameBackFloorsFinal[finalFloor];
}
}
public void floorSpawner()
{
nextPosition.x -= 232;
Instantiate(currentSpawnFloor, nextPosition, Quaternion.identity);
}
public void flatGroundSpawner()
{
nextPosition.x -= 232;
Instantiate(gameBackFloorsOne[0], nextPosition, Quaternion.identity);
}
}
