Я пытался поменять код так что бы мир генерировался сразу при активации сцены, после этих манипуляций он стал выдавать ошибку
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at :0)
GenerationManager.GenerateWorld () (at Assets/Scripts/GeneratorWorld/GenerationManager.cs:143)
GenerationManager.Start () (at Assets/Scripts/GeneratorWorld/GenerationManager.cs:54)
В коде смотрел обе эти строки и так и не понял что с ними не так, в обоих случаях никаких явных ошибок которые бы мог выявить именно Я(а я новичок в C#), я не нашёл и поэтому решил обратиться суда.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public enum GenerationState
{
Idle,
GeneratingRooms,
GeneratingLighting,
GeneratingSpawn,
GeneratingExit,
GeneratingBarrier,
}
public class GenerationManager : MonoBehaviour
{
[Header("References")]
[SerializeField] Transform WorldGrid;
public List<GameObject> RoomPrefabs;
[SerializeField] List<GameObject> LightTypes;
[SerializeField] int mapSize = 16;
[SerializeField] int Emptiness = 16;
[SerializeField] int Brightness = 16;
//[SerializeField] Slider MapSizeSlider, EmptinessSlider, BrightnessSlider;
//[SerializeField] Button GenerateButton;
[SerializeField] GameObject E_Room;
[SerializeField] GameObject B_Room;
[SerializeField] GameObject SpawnRoom, ExitRoom;
public List<GameObject> GeneratedRooms;
[SerializeField] GameObject PlayerObject, MainCameraObject;
[Header("Settings")]
public int mapEmptiness;
public int mapBrightness;
private int mapSizeSquare;
private Vector3 currentPos;
private float currentPosx, currentPosZ, currentPosTracker, currentRoom;
public float roomSize = 4.8f;
public GenerationState currentState;
private void Start()
{
mapSize = (int)Mathf.Pow(mapSize, 4);
mapSizeSquare = (int)Mathf.Sqrt(mapSize);
mapEmptiness = Emptiness;
mapBrightness = Brightness;
GenerateWorld();
SpawnPlayer();
}
//private void Update()
//{
// mapSize = (int)Mathf.Pow(mapSize, 4);
// mapSizeSquare = (int)Mathf.Sqrt(mapSize);
// mapEmptiness = Emptiness;
// mapBrightness = Brightness;
//}
public void ReloadWorld()
{
// SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void GenerateWorld()
{
for (int i = 0; i < mapEmptiness; i++)
{
RoomPrefabs.Add(E_Room);
}
//GenerateButton.interactable = false;
for (int state = 0; state < 6; state++)
{
for (int i = 0; i < mapSize; i++)
{
if (currentPosTracker == mapSizeSquare)
{
if (currentState == GenerationState.GeneratingBarrier) GenerateBarrier();
currentPosx = 0;
currentPosTracker = 0;
currentPosZ += roomSize;
if (currentState == GenerationState.GeneratingBarrier) GenerateBarrier();
}
currentPos = new(currentPosx, 0, currentPosZ);
switch (currentState)
{
case GenerationState.GeneratingRooms:
GeneratedRooms.Add(Instantiate(RoomPrefabs[Random.Range(0, RoomPrefabs.Count)], currentPos, Quaternion.identity, WorldGrid));
break;
case GenerationState.GeneratingLighting:
Instantiate(LightTypes[Random.Range(0, LightTypes.Count)], currentPos, Quaternion.identity, WorldGrid);
break;
case GenerationState.GeneratingBarrier:
if (currentRoom <= mapSizeSquare && currentRoom >= 0)
{
GenerateBarrier();
}
if (currentRoom <= mapSize && currentRoom >= mapSize - mapSizeSquare)
{
GenerateBarrier();
}
break;
}
currentRoom++;
currentPosTracker++;
currentPosx += roomSize;
}
NextState();
switch (currentState)
{
case GenerationState.GeneratingExit:
int roomToReplace = Random.Range(0, GeneratedRooms.Count);
GameObject exitRoom = Instantiate(ExitRoom, GeneratedRooms[roomToReplace].transform.position, Quaternion.identity, WorldGrid);
Destroy(GeneratedRooms[roomToReplace]);
GeneratedRooms[roomToReplace] = exitRoom;
break;
case GenerationState.GeneratingSpawn:
int _roomToReplace = Random.Range(0, GeneratedRooms.Count);
GameObject spawnRoom = Instantiate(SpawnRoom, GeneratedRooms[_roomToReplace].transform.position, Quaternion.identity, WorldGrid);
Destroy(GeneratedRooms[_roomToReplace]);
GeneratedRooms[_roomToReplace] = spawnRoom;
break;
}
}
}
public GameObject spawnRoom;
public void SpawnPlayer()
{
PlayerObject.SetActive(false);
PlayerObject.transform.position = new Vector3(SpawnRoom.transform.position.x, 1.8f, SpawnRoom.transform.position.z);
PlayerObject.SetActive(true);
MainCameraObject.SetActive(true);
}
public void NextState()
{
currentState++;
currentRoom = 0;
currentPosx = 0;
currentPosZ = 0;
currentPos = Vector3.zero;
currentPosTracker = 0;
}
public void Win()
{
PlayerObject.SetActive(false);
MainCameraObject.SetActive(true);
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
Debug.Log("Player has exited and won the Game!");
}
public void GenerateBarrier()
{
currentPos = new(currentPosx, 0, currentPosZ);
Instantiate(B_Room, currentPos, Quaternion.identity, WorldGrid);
}
}
Прошу помогите