要跟踪的对象是什么,你实例化时,你实例化一个对象,你可以在List
加它,你可以创建GameObject
的List
然后。然后,您可以使用该列表中的任何GameObject
来执行您所需的任何操作。
public GameObject[] arrows;
public float interval = 5;
private List<GameObject> myObjects = new List<GameObject>();
// Use this for initialization
void Start()
{
StartCoroutine(SpawnArrow());
}
public IEnumerator SpawnArrow()
{
WaitForSeconds delay = new WaitForSeconds(interval);
while (true)
{
GameObject prefab = arrows[UnityEngine.Random.Range(0, arrows.Length)];
GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity);
//Add the object to the list
myObjects.Add(clone);
yield return delay;
}
}
而且像,这将是更好地使用InvokeRepeating
产卵的对象,你可以通过它的重复率,当你希望它开始。 More information on InvokeRepeating
这是怎么可能看起来像InvokeRepeating
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()
{
//Check if there's something in the list
if (myObjects.Count > 0)
{
//Destroy the last object in the list.
Destroy(myObjects.Last());
//Remove it from the list.
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);
//Add the object to the list
myObjects.Add(clone);
}
您可以通过执行Destroy(myObjects.Last());
将返回的最后一个对象在列表中,并摧毁它摧毁最后催生对象在列表中。然后您应该从列表中删除它myObjects.RemoveAt(myObjects.Count - 1);
您只需使用您创建的对象作为参数。你在while循环中放入一个 'YourOtherFunction(clone)'。如果这不是你正在寻找的东西,你需要提供更多关于你想要做的事情的信息。在您的代码示例中也包含其他功能。 –
另外,如果你问的是关于Unity的问题,你需要在你的问题上加上[tag:Unity3d]标签。否则,你可能会得到不适合在Unity上使用的答案。 –