【问题标题】:Content of List gets Lost between start() and update()List 的内容在 start() 和 update() 之间丢失
【发布时间】: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);
        }
    }
}

【问题讨论】:

  • 欢迎来到变量作用域的主题

标签: c# list unity3d indexing


【解决方案1】:

您将它们存储在Start中的局部变量

List<int> possibleTiles = new List<int>();
for (int x = 0; x < bounds.size.x; x++) {
...

隐藏同名字段。因此,您在Start 中所做的更改不会更改您的Update 方法尝试访问的全局已知列表possibleTiles

宁可使用您已有的字段并正确初始化它

public List<int> possibleTiles = new List<int>();

Start 中只做

possibleTiles.Clear();
for (int x = 0; x < bounds.size.x; x++) {
...

【讨论】:

    【解决方案2】:

    给你,这可能就是你想要的

    public class Spawner : MonoBehaviour
    {
        private GameObject[] getCount;
        private int count;
        public int maxBags = 1;
        public Transform goldBag;
        public List<int> possibleTiles = new List<int>(); // Initialized list holding possible tiles
        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);
            // Remove the line below since we want to declare the list as member variable and we already initialized it
            //List<int> possibleTiles = new List<int>();
            
            possibleTiles.Clear();
            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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-13
      • 2020-07-08
      • 2015-11-27
      • 2017-02-14
      • 1970-01-01
      • 2016-01-11
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多