代码之家  ›  专栏  ›  技术社区  ›  Lee Jaedong

场景开始时的随机数组或列表大小

  •  -1
  • Lee Jaedong  · 技术社区  · 6 年前

     public GameObject[] blocks;
    

    然而,如果我尝试随机范围与数组,我会得到各种各样的错误。与列表相同<>。

    我希望我的场景以随机数量的项目(在一个范围内)开始。

    有什么建议吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Ruzihm aks786    6 年前

    你需要实例化游戏对象

    public int minRandomSize = 1;
    public int maxRandomSize = 20;
    public GameObject blockPrefab;
    
    public void Start() {
        int size = Random.Range(minRandomSize,maxRandomSize+1);
        blocks = new GameObject[size];
        for (int i = 0 ; i < size; i++) {
            blocks[i] = Instantiate(blockPrefab);
    
            // do stuff with blocks[i] to make that block different than the rest
        }
    }