你好,我在制作我的第一个2D Unity游戏,现在有以下代码。我有3个不同的游戏对象(称之为a、b、c),它们都在名为arrows的数组中,我想知道哪一个被生成了(这样我就可以在另一个函数中使用它)并从场景中删除先前的对象。现在它只是每隔5秒在另一个对象上生成一个游戏对象,我不知道它随机生成了其中的哪一个。有什么想法吗?
public GameObject[] arrows;
public float interval = 5;
// 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);
yield return delay;
}
}`
YourOtherFunction(clone)
。如果这不是你想要的,请提供更多关于你尝试做什么的细节。在你的代码示例中也包括其他函数。 - Scott Chamberlain