阻止敌人产生Unity5的方法

时间:2017-11-28 09:18:53

标签: c# unity3d unity5

我正在开发激光防御者游戏。

这是负责产生敌人的主要功能。我将变量波设置为3.然后我想破坏太空船的位置以便不允许它们产卵。问题是当我开始游戏时根本没有船只。

if (AllMembersDead())
{
    for (float i = 0; i < waves; i++)
    {
        SpawnUntilFull();       // number of waves to be spawned
    }
    Destroy(position);
}

这是我毁灭的游戏对象的图片:

enter image description here

如果需要,这是SpawnUntilFull()和AllMembersDead()的函数。

bool AllMembersDead()
{
    foreach(Transform childPositionGameObject in transform)
    {
        if (childPositionGameObject.childCount > 0)
        {
            return false;
        }
    }
    return true;
}

void SpawnUntilFull()
{
    Transform freePosition = NextFreePosition();
    if (freePosition)
    {
        GameObject enemy = Instantiate(enemyPrefab, freePosition.transform.position, Quaternion.identity) as GameObject; // Instantiate (spawning or creating) enemy prefab (as gameobject assures that what its returning to us is no normal object but rather a game object)
        enemy.transform.parent = freePosition; // the transform of whatever thing the enemy is attached to, and that would be the enemyFormation GameObject
    }

    if (NextFreePosition())
    {
        Invoke("SpawnUntilFull", spawnDelay);
    }
}

我不确定我做错了什么。任何指导都将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为您的 AllMembersDead()函数返回false,因为您正在查看的 childPositionGameObject 是TotalPositions游戏对象,因此 SpawnUntilFull()永远不会被称为。 尝试将函数更改为指向正确的父级,如下所示:

bool AllMembersDead()
{
    foreach(Transform childPositionGameObject in position)
    {
        if (childPositionGameObject.childCount > 0)
        {
            return false;
        }
    }
    return true;
}