Unity-创建板时参数超出范围

本文关键字:范围 参数 创建 Unity- | 更新日期: 2023-09-27 18:00:06

这是我的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)

我不知道如何修复它…

Unity-创建板时参数超出范围

tl;drpositions从未初始化为空列表以外的任何内容,因此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是一个空列表。