1
這裏是當我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)
我不知道我怎麼能解決這個問題....