I have a really weird issue in my LevelGenerator script. Pretty much I’ve been following a level generating tutorial by CodeMonkey and everything was working completely fine and the script was working perfectly. I then wanted to move the exact same script into a different project that I’ve been working on but then I’ve been hit with the following error. (didn’t change anything at all about the script in the other project)
If the image doesn’t work >
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 <437ba245d8404784b9fbab9b439ac908>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <437ba245d8404784b9fbab9b439ac908>:0)
LevelGenerator.SpawnLevelPart () (at Assets/Scripts/LevelGenerator.cs:72)
LevelGenerator.Awake () (at Assets/Scripts/LevelGenerator.cs:39)
The strange thing is is that it works perfectly fine in one project but spits out this error in a different project. Any help on this issue would be appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelGenerator : MonoBehaviour
{
private const float PLAYER_DISTANCE_SPAWN_LEVEL_PART = 10f;
[SerializeField] private Transform levelPart_Start;
[SerializeField] private Transform pfTestingPlatform;
[SerializeField] private List<Transform> levelPartEasyList;
[SerializeField] private List<Transform> levelPartMediumList;
[SerializeField] private List<Transform> levelPartHardList;
[SerializeField] private List<Transform> levelPartExtremeList;
[SerializeField] private PlayerController player;
private enum Difficulty
{
Easy,
Medium,
Hard,
Extreme
}
private Vector3 lastEndPosition;
private int levelsPartsSpawned;
private void Awake()
{
lastEndPosition = levelPart_Start.Find("EndPosition").position;
if(pfTestingPlatform !=null)
{
Debug.Log("Debugging Platforms");
}
int startingSpawnLevelParts = 5;
for(int i = 0; i < startingSpawnLevelParts; i++)
{
SpawnLevelPart();
}
}
private void Update()
{
if(Vector3.Distance(player.GetPosition(), lastEndPosition) < PLAYER_DISTANCE_SPAWN_LEVEL_PART)
{
SpawnLevelPart();
}
}
private void SpawnLevelPart()
{
List <Transform> difficultyLevelPartList;
switch(GetDifficulty())
{
default:
case Difficulty.Easy: difficultyLevelPartList = levelPartEasyList; break;
case Difficulty.Medium: difficultyLevelPartList = levelPartMediumList; break;
case Difficulty.Hard: difficultyLevelPartList = levelPartHardList; break;
case Difficulty.Extreme: difficultyLevelPartList = levelPartExtremeList; break;
}
Transform chosenLevelPart = difficultyLevelPartList[Random.Range(0, difficultyLevelPartList.Count)];
if (pfTestingPlatform != null)
{
chosenLevelPart = pfTestingPlatform;
}
Transform lastLevelPartTransform = SpawnLevelPart(chosenLevelPart, lastEndPosition);
lastEndPosition = lastLevelPartTransform.Find("EndPosition").position;
levelsPartsSpawned++;
}
private Transform SpawnLevelPart(Transform levelPart, Vector3 spawnPosition)
{
Transform levelPartTransform = Instantiate(levelPart, spawnPosition, Quaternion.identity);
return levelPartTransform;
}
private Difficulty GetDifficulty()
{
if (levelsPartsSpawned >= 15) return Difficulty.Extreme;
if (levelsPartsSpawned >= 10) return Difficulty.Hard;
if (levelsPartsSpawned >= 5) return Difficulty.Medium;
return Difficulty.Easy;
}
}