【发布时间】:2020-07-03 09:29:03
【问题描述】:
我尝试使用来自 tilemap 的图块创建一个列表,并使用该列表获取 tilemap 上的图块坐标。 我想通过查看更新函数中的 cmets 问题很明显,但我不知道该怎么办
public class Spawner : MonoBehaviour
{
private GameObject[] getCount;
private int count;
public int maxBags = 1;
public Transform goldBag;
public List<int> possibleTiles;
public int possibleTilesLength;
private int randomTileNr;
public List<int> copyOfPossibleTiles;
void Start()
{
Tilemap tilemap = GetComponent<Tilemap>();
BoundsInt bounds = tilemap.cellBounds;
TileBase[] allTiles = tilemap.GetTilesBlock(bounds);
List<int> possibleTiles = new List<int>();
for (int x = 0; x < bounds.size.x; x++) {
for (int y = 0; y < bounds.size.y; y++) {
TileBase tile = allTiles[x + y * bounds.size.x];
if (tile != null) {
possibleTiles.Add(x);
Debug.Log(possibleTiles.Count);
possibleTiles.Add(y);
Debug.Log(possibleTiles.Count);
possibleTilesLength = possibleTiles.Count;
}
}
}
}
void Update()
{
getCount = GameObject.FindGameObjectsWithTag("bagCoins");
if(getCount.Length < maxBags)
{
randomTileNr = (int)UnityEngine.Random.Range(0,possibleTilesLength/2)*2;
Debug.Log(possibleTiles.Count); //prints 0
Debug.Log(possibleTilesLength); //prints 128
Debug.Log(randomTileNr); //prints random even number between 0 and 128
Debug.Log(copyOfPossibleTiles[randomTileNr]); //prints error index out of range.
maxBags = 0;
Instantiate(goldBag, new Vector3(possibleTiles[randomTileNr],possibleTiles[randomTileNr+1],0), Quaternion.identity);
}
}
}
【问题讨论】:
-
欢迎来到变量作用域的主题