在空的空间中实例化硬币

时间:2015-01-20 01:51:24

标签: c# unity3d

我正在开发一个小型Unity2D游戏,基本上我想检查可用空间来实例化两个硬币。我已经建立了一个协程,但由于某种原因,我没有实例化硬币,因为我检查它们是否达到了任何界限。

这是我的协程

    private IEnumerator InsCoins(float secondsBetweenEachIns)
{
    while (true)
    {
        float tmp1X = float.Parse(Random.Range(minX, maxX).ToString());
        float tmp1Y = float.Parse(Random.Range(minX, maxX).ToString());
        float tmp2X = float.Parse(Random.Range(minX, maxX).ToString());
        float tmp2Y = float.Parse(Random.Range(minX, maxX).ToString());

        Vector3 ins1 = new Vector3(tmp1X, tmp1Y);
        Vector3 ins2 = new Vector3(tmp2X, tmp2Y);

        int count = GameObject.FindObjectsOfType<GameObject>().Length;
        int arrayCount = 0;
        Bounds[] getBound = new Bounds[count];
        foreach (GameObject go in GameObject.FindObjectsOfType<GameObject>())
        {
            getBound[arrayCount] = go.renderer.bounds;
            arrayCount++;
        } 
        GameObject tmp1 = (GameObject)GameObject.Instantiate(coin, new Vector3(tmp1X, tmp1Y, 0), Quaternion.identity);
        GameObject tmp2 = (GameObject)GameObject.Instantiate(redcoin, new Vector3(tmp2X, tmp2Y, 0), Quaternion.identity);

        foreach (Bounds go in getBound)
        {
            do
            {
                if (go.Intersects(tmp1.renderer.bounds) || go.Intersects(tmp2.renderer.bounds))
                {
                    Destroy(tmp1); Destroy(tmp2);
                }
            } while (!go.Intersects(tmp1.renderer.bounds) && go.Intersects(tmp2.renderer.bounds));
        }
        yield return new WaitForSeconds(secondsBetweenEachIns);
    }
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

也许你没有正确地调用协同程序。你在哪里以及如何打电话InsCoins

如果你想打电话给这个协程,你只需这样打电话:

StartCoroutine(InsCoins(2.0)); // For InsCoins with 2 sec between each