这是我的BoardGenerator.cs
using UnityEngine;
using System;
using System.Collections.Generic;
using Random = UnityEngine.Random;
public class BoardGenerator : MonoBehaviour {
[Serializable]
public class Count{
public int min;
public int max;
public Count(int m, int M){
min = m;
max = M;
}
}
public int columns = 20;
public int rows = 20;
public Count wallCount = new Count (10, 20);
public Count moneyCount = new Count (10, 15);
public GameObject floorTiles;
public GameObject wallTiles;
public GameObject moneyTiles;
public GameObject[] enemyTiles;
private Transform board;
private List <Vector3> positions = new List<Vector3>();
void BoardSetup(){
board = new GameObject ("Board").transform;
for (int x=-1; x<columns+1; x++) {
for (int y=-1; y<rows+1; y++){
GameObject toInstantiate = floorTiles;
if(x==-1||x==columns||y==-1||y==rows)
toInstantiate = wallTiles;
GameObject instance = Instantiate (toInstantiate, new Vector3 (x, y, 0f), Quaternion.identity) as GameObject;
instance.transform.SetParent (board);
}
}
}
Vector3 RandPos(){
int randomIndex = Random.Range (0, positions.Count);
Vector3 randPos = positions [randomIndex];
positions.RemoveAt (randomIndex);
return randPos;
}
void MoneyWallRandom (GameObject tileObj, int m, int M)
{
int objectCount = Random.Range (m, M);
for(int i = 0; i < objectCount; i++)
{
Vector3 randomPosition = RandPos();
GameObject tile = tileObj;
Instantiate(tile, randomPosition, Quaternion.identity);
}
}
void EnemyRandom (GameObject[] tileObj, int m, int M)
{
int objectCount = Random.Range (m, M);
for(int i = 0; i < objectCount; i++)
{
Vector3 randomPosition = RandPos();
GameObject tile = tileObj[Random.Range (0, tileObj.Length)];
Instantiate(tile, randomPosition, Quaternion.identity);
}
}
public void BoardSetup (int level)
{
BoardSetup ();
MoneyWallRandom (wallTiles, wallCount.min, wallCount.max);
MoneyWallRandom (moneyTiles, moneyCount.min, moneyCount.max);
int enemyCount = level + 3;
EnemyRandom (enemyTiles, enemyCount, enemyCount);
}
}
我的GameManager.cs
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public BoardGenerator boardScript;
private int level = 1;
// Use this for initialization
void Awake () {
boardScript = GetComponent<BoardGenerator> ();
InitGame ();
}
void InitGame(){
boardScript.BoardSetup (level);
}
// Update is called once per frame
void Update () {
}
}
董事会成功创建,但由于某些原因无法创建敌人和金钱牌。 以下是控制台中的错误
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-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
BoardGenerator.RandPos () (at Assets/_Scripts/BoardGenerator.cs:50)
BoardGenerator.MoneyWallRandom (UnityEngine.GameObject tileObj, Int32 m, Int32 M) (at Assets/_Scripts/BoardGenerator.cs:61)
BoardGenerator.BoardSetup (Int32 level) (at Assets/_Scripts/BoardGenerator.cs:83)
GameManager.InitGame () (at Assets/_Scripts/GameManager.cs:18)
GameManager.Awake () (at Assets/_Scripts/GameManager.cs:14)
我不知道如何解决它......
答案 0 :(得分:1)
tl; dr positions
永远不会被初始化为空列表以外的任何内容,因此randomIndex
始终为0,因此在抛出异常的行中,您正尝试删除索引0处的项目,但列表中没有项目,因此会引发异常。
正确初始化positions
并解决您的问题。
在这一行抛出异常:
positions.RemoveAt (randomIndex);
这种情况正在发生,因为positions.Count
为0所以
int randomIndex = Random.Range (0, positions.Count);
相当于
int randomIndex = Random.Range (0, 0);
相当于
int randomIndex = 0;
在任何索引处都无法从positions
中删除任何内容,因为positions
是一个空列表。