***This is a long one, so prepare for a long read.***
Quite simply, I'm following the Roguelike Tutorial series on the official Unity YouTube Channel, and on Part 5, the scripts used to generate the level are being used to, of course, generate the level. In the tutorial, this works, but when I do it, nothing happens, just a clear blue screen as the game. The error message I get is *'ArgumentOutOfRangeException: Argument is out of range.'* and I'm not sure what I'm meant to do to fix this. I'll put the code and all of the text alongside the error message (I assume the text that tells me what I messed up on), and hopefully someone knows what to do to fix this.
***Code 1 (No clue why some isn't in that window with the rest of the code)***
using UnityEngine;
using System;
using System.Collections.Generic;
using Random = UnityEngine.Random;
public class BoardManager : MonoBehaviour {
[Serializable]
public class Count
{
public int minimum;
public int maximum;
public Count(int min, int max)
{
minimum = min;
maximum = max;
}
}
public int columns;
public int rows;
public Count wallCount = new Count (5, 9);
public Count powerCount = new Count (1, 5);
public GameObject exit;
public GameObject[] floorTiles;
public GameObject[] wallTiles;
public GameObject[] powerTiles;
public GameObject[] enemyTiles;
public GameObject[] exteriorWallTiles;
private Transform boardHolder;
private List gridPositions = new List();
void InitialiseList()
{
gridPositions.Clear();
for (int x = 1; x < columns - 1; x++)
{
for (int y = 1; x < columns - 1; y++)
{
gridPositions.Add(new Vector3(x, y, 0f));
}
}
}
void BoardSetup()
{
boardHolder = new GameObject("Board").transform;
for (int x = -1; x < columns - 1; x++)
{
for (int y = -1; x < columns - 1; y++)
{
GameObject toInsantiate = floorTiles[Random.Range(0, floorTiles.Length)];
if (x == -1 || x == columns || y == -1 || y == rows)
toInsantiate = exteriorWallTiles[Random.Range(0, exteriorWallTiles.Length)];
GameObject instance = Instantiate(toInsantiate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject;
instance.transform.SetParent(boardHolder);
}
}
}
Vector3 RandomPosition()
{
int randomIndex = Random.Range(0, gridPositions.Count);
Vector3 RandomPosition = gridPositions[randomIndex];
gridPositions.RemoveAt(randomIndex);
return RandomPosition;
}
void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maximum)
{
int objectCount = Random.Range(minimum, maximum + 1);
for (int i = 0; i < objectCount; i++)
{
Vector3 randomPosition = RandomPosition();
GameObject tileChoice = tileArray[Random.Range(0, tileArray.Length)];
Instantiate(tileChoice, randomPosition, Quaternion.identity);
}
}
public void SetupScene (int level)
{
BoardSetup();
InitialiseList();
LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum);
LayoutObjectAtRandom(powerTiles, powerCount.minimum, powerCount.maximum);
int enemyCount = (int)Mathf.Log (level, 2f);
LayoutObjectAtRandom(enemyTiles, enemyCount, enemyCount);
Instantiate(exit, new Vector3(columns - 1, rows - 1, 0f), Quaternion.identity);
}
}
***Code 2***
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public BoardManager boardScript;
private int level = 3;
// Use this for initialization
void Awake()
{
boardScript = GetComponent();
InitGame();
}
void InitGame()
{
boardScript.SetupScene(level);
}
// Update is called once per frame
void Update () {
}
}
***Error Message***
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[UnityEngine.Vector3].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
BoardManager.RandomPosition () (at Assets/Scripts/BoardManager.cs:69)
BoardManager.LayoutObjectAtRandom (UnityEngine.GameObject[] tileArray, Int32 minimum, Int32 maximum) (at Assets/Scripts/BoardManager.cs:79)
BoardManager.SetupScene (Int32 level) (at Assets/Scripts/BoardManager.cs:90)
GameManager.InitGame () (at Assets/Scripts/GameManager.cs:21)
GameManager.Awake () (at Assets/Scripts/GameManager.cs:16)
Also I'm new to Unity and pretty much any game development, so if this seems like a simple mistake that I made, that's probably why.
Thanks in advance :)
↧