简体   繁体   中英

Unity - Argument is out of range when creating board

Here's is my 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);
    }
}

And my 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 () {

    }
}

The board is created successfully but enemies and money tiles can't be created for some reasons. Here are the errors in console

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)

I have no idea how I can fix it....

tl;dr positions is never being initialized to anything other than an empty list so randomIndex is always 0, so at the line where the exception is being thrown, you are trying to remove an item at index 0, but there are no items in the list, so it throws an exception.

Initialize positions properly and you solve your problem.


It is throwing the exception at this line:

positions.RemoveAt (randomIndex);

This is happening because positions.Count is 0 so

int randomIndex = Random.Range (0, positions.Count);

is equivalent to

int randomIndex = Random.Range (0, 0);

is equivalent to

int randomIndex = 0;

there's nothing to remove from positions at any index, because positions is an empty list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM