简体   繁体   English

Respawn Destroyed GameObject(Enemy)

[英]Respawn Destroyed GameObject (Enemy)

I am writing a game (2D platformer) and I'm trying to get enemies to respawn when the player respawns. 我正在写一个游戏(2D平台游戏),当玩家重生时,我试图让敌人重生。 I cannot simply reload the level as I have some objects that load when the level loads, so it would cause some problems. 我不能简单地重新加载关卡,因为我有一些在加载关卡时加载的对象,所以它会导致一些问题。 Instead, to respawn the player, I have it returned to its starting location (and I handle the lost life and other details). 相反,要重新生成玩家,我让它返回到它的起始位置(我处理丢失的生命和其他细节)。

A player destroys an enemy by hitting it on its head, as follows (in OnTriggerEnter on the player): 玩家通过击中头部来摧毁敌人,如下所示(在玩家的OnTriggerEnter中):

if(otherObject.CompareTag("Minion")) //hit by minion
    {
        if(hitFromTop(otherObject.gameObject)) //if player jumped on enemy
        {
            otherObject.GetComponent<Minion>().setMoving(false); //stop moving
            playSound(KillEnemySound); //play killing enemy sound
            jump();
            Destroy(otherObject.gameObject); //kill minion

        }
                    //else hurt player
    }

As you can see, I destroy the enemy object entirely. 如你所见,我完全摧毁了敌人的物体。 In order to maintain which enemies are where, I add them to a list (stored in a separate GameObject) on creation. 为了保持哪些敌人在哪里,我在创建时将它们添加到列表(存储在单独的GameObject中)。 The list is created in the separate enemy respawning object, as follows: 该列表在单独的敌人重生对象中创建,如下所示:

void Start ()
{
    enemyList = GameObject.FindGameObjectsWithTag("Minion");
    Debug.Log ("Adding all minions to list");
}

I'm attempting to call a function respawning all minions in the list at their original location by going through the list. 我试图通过列表调用一个函数来重新生成列表中所有minions的原始位置。 The function is as follows: 功能如下:

public void RespawnAll()
{
    foreach(GameObject minion in enemyList)
    {
        Destroy(minion); //make sure to respawn ALL minions
    }
    Debug.Log ("Respawning all");
    foreach(GameObject minion in enemyList)
    {
        Debug.Log ("instantiating minions from list");
        Instantiate (minion, minion.GetComponent<Minion>().origPosition, Quaternion.identity);
    }
}

I know that it isn't the most time-optimal method to delete all enemies and respawn them all, and if this logic is wrong or if you know a better way, I'm open to new ideas. 我知道删除所有敌人并重新生成所有敌人并不是时间最优的方法,如果这种逻辑错误或者你知道更好的方法,我会接受新的想法。

The problem with this idea is, I get an error: 这个想法的问题是,我收到一个错误:

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

It seems I'm adding a reference to the existing minion to the list rather than a copy. 似乎我在列表中添加了对现有minion的引用而不是副本。 How can I respawn the enemies properly, at their original position? 我怎样才能在原来的位置上正确地重建敌人?

I took Jerdak's advice and, instead of Destroying the enemies, I disabled them. 我接受了杰达克的建议,而不是摧毁敌人,而是禁用了他们。 This way, they still existed, and I could loop through and re-enable all of the enemies that were disabled (killed). 这样,它们仍然存在,我可以循环并重新启用所有被禁用(被杀死)的敌人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM