0
\$\begingroup\$

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);
    }

}

Inspector Image

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

I can't say for sure, but in here:

unroundedTwoFloor = Random.Range(-0.6f, 3.4f);
twoFloor = Mathf.RoundToInt(unroundedTwoFloor);

unroundedTwoFloor could very well be -0.6f, which means that twoFloor could become -1.

\$\endgroup\$
1
  • \$\begingroup\$ That Oddly worked. I stopped getting out of range errors in my console. Thanks \$\endgroup\$ Aug 3, 2018 at 0:58
0
\$\begingroup\$

To fix the out of range issue I changed the two unrounded values to something that would correctly round to a correct value; New 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.4f, 3.4f);
        twoFloor = Mathf.RoundToInt(unroundedTwoFloor);

        unroundedFinalFloor = Random.Range(-0.4f, 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);
    }

}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .