【发布时间】:2018-03-10 22:29:01
【问题描述】:
好的,我正在尝试做两件事:
1- 预制件一旦生成,就不应再次生成
2- 生成点一旦用于预制件,就不应再次使用(因此两个对象不会在同一位置生成)
一直在尝试用列表来做到这一点。我遇到的问题是在 while 循环中删除列表中的正确项目,这似乎是有问题的。这些对象都在同一个生成点生成,我不知道为什么。
这是我的代码:
public List<GameObject> myList = new List<GameObject>();
public List<GameObject> spawnPoint = new List<GameObject>();
void Start () {
Debug.Log ("Total objects to spawn now is " + myList.Count);
if(myList.Count == 0){
Debug.Log ("No Objects to spawn");
} else {
if(spawnPoint.Count ==0)
{
Debug.Log ("No more spawn points");
} else {
while (myList.Count > 0) {
GameObject itemToSpawn = myList [Random.Range (0, myList.Count)];
GameObject spawns = spawnPoint [Random.Range (0, spawnPoint.Count)];
Vector3 position = GameObject.FindGameObjectWithTag ("replace").transform.position;
GameObject myItem = Instantiate (itemToSpawn, position, Quaternion.identity) as GameObject;
myList.Remove (itemToSpawn);
Debug.Log ("Total item now is " + myList.Count);
spawnPoint.Remove (spawns);
Debug.Log ("Total spawn points now is " + spawnPoint.Count);
DestroyObject (GameObject.FindGameObjectWithTag ("replace"));
}
}
}
}
}
【问题讨论】:
-
是使用堆栈或队列而不是列表的更好方法。您可以简单地学习使用它。但你应该知道这些类型是不可序列化的,不会显示在检查器中。