【发布时间】:2017-04-12 14:46:06
【问题描述】:
我在 Unity 中使用 C#。我在数组中有 3 个不同的 GameObjects,我想使用这样的 if 语句将最后一个生成/添加的对象添加到列表中:If object 1 spawned then ...
我该如何进行这项工作?这是代码:
public GameObject[] arrows;
public float interval = 5;
private List<GameObject> myObjects = new List<GameObject>();
// Use this for initialization
void Start()
{
InvokeRepeating("SpawnArrow", 0f, interval);
}
void SpawnArrow()
{
if (myObjects.Count > 0)
{
Destroy(myObjects.Last());
myObjects.RemoveAt(myObjects.Count - 1);
}
GameObject prefab = arrows[UnityEngine.Random.Range(0, arrows.Length)];
GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity);
myObjects.Add(clone);
}
【问题讨论】: