Argument out of Range Exception Error. [Basic]
Hello, I am attempting to follow along with a unity tutorial to create a basic free-runner game. Within my script written for a basic object pooling ground generator I meet no apparent errors within my script. For context, this script is attached to an empty game object that should auto generate prefab platforms, moving forward and randomize designated obstacle's appearance. However, upon launching the scene I am met with this error inside unity's console:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <fb001e01371b4adca20013e0ac763896>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <fb001e01371b4adca20013e0ac763896>:0)
GroundGenerator.Update () (at Assets/Scripts/GroundGenerator.cs:88)
I have researched this error and understand that this is a common error and is related to my use of arrays, however in studying past solutions to this error I am struggling to convert it into solving my issue. Furthermore, I have compared my script with that provided by the tutorial (link) and cannot see any apparent differences that generate this error. Moreover, I have followed the console's error pointing to line 88, however that line is simply a }.
Apologies if this is a simple fix that I'm easily missing and wasting forum discussion space, this is only my second week working with unity and C#, and after 2 days staring at this error, I'm only becoming more confused. I highly appreciate any insight anyone can provide into how to correct this error.
my code:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GroundGenerator : MonoBehaviour
{
public Camera mainCamera;
public Transform startPoint; // point from where ground tile will begin
public PlatformTile tilePrefab;
public float movingSpeed = 12;
public int tilestoPreSpawn = 15; // how many tiles should be pre-spawned
public int tilesWithoutObstacles = 3; //How many tiles at the beginning should not have obstacles, good for warm-up
List<PlatformTile> spawnedTiles = new List<PlatformTile>();
int nextTileToActivate = -1;
[HideInInspector]
public bool gameOver = false;
static bool gameStarted = false;
float score = 0;
public static GroundGenerator instance;
void Start()
{
instance = this;
Vector3 spawnPosition = startPoint.position;
int tilesWithNoObstaclesTmp = tilesWithoutObstacles;
for (int i = 0; i < tilestoPreSpawn; i++)
{
spawnPosition -= tilePrefab.startPoint.localPosition;
PlatformTile spawnedTile = Instantiate(tilePrefab, spawnPosition, Quaternion.identity) as PlatformTile;
if (tilesWithNoObstaclesTmp > 0)
{
spawnedTile.DeactivateAllObstacles();
tilesWithNoObstaclesTmp--;
}
else
{
spawnedTile.ActivateRandomObstacle();
}
spawnPosition = spawnedTile.endPoint.position;
spawnedTile.transform.SetParent(transform);
spawnedTiles.Add(spawnedTile);
}
}
void Update()
{
//moves the object upward in world space * unit/second
// increasing speed increases the score
if (!gameOver && gameStarted)
{
transform.Translate(-spawnedTiles[0].transform.forward * Time.deltaTime * (movingSpeed + (score / 500)), Space.World);
score += Time.deltaTime * movingSpeed;
}
if (mainCamera.WorldToViewportPoint(spawnedTiles[0].endPoint.position).z < 0)
{
// move the tile to the front if it's being the camera
PlatformTile tiletmp = spawnedTiles[0];
spawnedTiles.RemoveAt(0);
tiletmp.transform.position = spawnedTiles[spawnedTiles.Count - 1].endPoint.position - tiletmp.startPoint.localPosition;
tiletmp.ActivateRandomObstacle();
spawnedTiles.Add(tiletmp);
}
if (gameOver || !gameStarted)
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (gameOver)
{
//restart current scene
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}
else
{
//start the game
gameStarted = true;
}
}
}
}
void OnGUI()
{
if (gameOver)
{
GUI.color = Color.red;
GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200), "Game Over\nYour score is: " + ((int)score) + "\nPress 'Space' to restart");
}
else
{
if (!gameStarted)
{
GUI.color = Color.red;
GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200), "Press 'Space' to start");
}
}
GUI.color = Color.green;
GUI.Label(new Rect(5, 5, 200, 25), "Score: " + ((int)score));
}
}
Your answer
Follow this Question
Related Questions
0xc000007b error on 64 bit machine? 1 Answer
2018.2 text mesh pro. plugin Errors. 7 Answers
AndroidArchitecture' does not contain a definition for 'X86' 1 Answer
PLEASE HELP, BUILD APK ERROR 0 Answers