简体   繁体   English

在 Unity 中销毁克隆

[英]Destroying Clone in Unity

Hi everyone Im a bit stuck here.大家好,我有点卡在这里。 I have my gameObject Enemy Spawn at a random time.我在随机时间生成我的游戏对象敌人。 But the thing is that I only want the Enemy to be in the game for say 5 seconds.但问题是我只希望敌人在游戏中停留 5 秒。 The trouble is that I can't destroy the object at all.问题是我根本无法销毁对象。 Here I wrote this code to try and destroy the Enemy Object:在这里,我编写了这段代码来尝试销毁敌人对象:

public class SpawnManager : MonoBehaviour {

public GameObject Enemy;
public float mytimer;
public float enemyHealth = 5.0f;

void Start()
{
    GameObject player = GameObject.Find("Player");
}

void spawnEnemy() {
    Transform enemy;
    GameObject enemySpawnPoint = GameObject.Find("EnemySpawn");
    enemy =  Instantiate(Enemy,enemySpawnPoint.transform.position,enemySpawnPoint.transform.rotation) as Transform; 
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.name == "EnemyTrigger") {
        mytimer = Random.Range(0,10);
        //Debug.Log("Now Destroying");
        Invoke("spawnEnemy", mytimer);
        Debug.Log("Spawn Normal");

        if(Enemy.name == "BloodyMary(Clone"){
            Destroy(Enemy, enemyHealth);
            Debug.Log("Now Destroying");
        }
        }
    }

    }

Everytime I run into the trigger it spawns a "BloodyMary(Clone)" which I am trying to destroy.每次我遇到触发器时,它都会生成一个“血腥玛丽(克隆)”,我试图将其销毁。 Any advice?有什么建议吗?

In my eyes it would make more sense for the enemy to handle its own destruction.在我看来,让敌人来处理自己的毁灭会更有意义。 This would mean that an enemy is responsible for its own duration which I feel would make more sense if you're having possibly a ton of enemies on the scene at any time.这意味着敌人对其自身的持续时间负责,我认为如果您随时可能在现场有大量敌人,这会更有意义。

I would create a co-routine which would simply wait for 5 seconds and then call the Destroy(gameObject) function to destroy itself.我会创建一个协程,它会简单地等待 5 秒,然后调用Destroy(gameObject)函数来销毁自己。 It may look a little like this:它可能看起来像这样:

IEnumerator DeathTimer(float duration)
{
    yeild return new WaitForSeconds(duration);
    Destroy(gameObject);
}

Then inside your 'Start()' method I would then call the co-routine (not done as a normal method).然后在你的 'Start()' 方法中,我将调用协同程序(不是作为普通方法完成的)。 This would be done using something like this:这将使用这样的东西来完成:

void Start()
{
    // calls the coroutine to start
    StartCoroutine("DeathTimer", duration);
}

Note: By calling a co-routine using a string (like above) you can then call "StopCoroutine("MethodName");"注意:通过使用字符串(如上)调用协同例程,您可以调用“StopCoroutine("MethodName");” which will stop the coroutine at any time.这将随时停止协程。 This would be better than passing in a method parameter into the StartCoroutine().这比将方法参数传入 StartCoroutine() 更好。

This means your spawner is now purely responsible for spawning the enemies and they are responsible for their own death if they last for too long.这意味着你的刷怪箱现在完全负责生成敌人,如果他们持续太久,他们要为自己的死亡负责。 Then you're not trying to manage multiple enemies on the spawner and you don't need to worry about tracking them in there.这样您就不必尝试在刷怪箱上管理多个敌人,也无需担心在那里跟踪它们。

I had a friend ask a very similar question the other day and he used this and it worked a treat.前几天我有一个朋友问了一个非常相似的问题,他用了这个,效果很好。

The code that says:代码说:

Invoke ("spawnEnemy", myTimer);

Is calling your spawnEnemy() function, which creates anther Enemy clone.正在调用您的spawnEnemy()函数,该函数会创建spawnEnemy() Enemy 克隆。 It also does the call with a delay timer.它还使用延迟计时器进行呼叫。

If you don't want another Enemy just remove that part of the code .如果您不想要另一个 Enemy,只需删除该部分代码即可


Also you are destroying with a timer.你也在用计时器销毁。 You can Destroy instantaneously with Destroy(Enemy) .您可以使用Destroy(Enemy)立即Destroy(Enemy)

I see a typo, as you missed a closing parethesis on the name check.我看到一个拼写错误,因为您在姓名检查中错过了一个结束的附加词。 the reason that the linter didnt pick this up was because the parenthesis is in the string, so to the program it seems perfectly reasonable to check for "bloodymary(clone" instead of "bloodymary(clone)". linter 没有选择它的原因是因为括号在字符串中,所以对于程序来说,检查“bloodymary(clone)”而不是“bloodymary(clone)”似乎是完全合理的。

Solution is straight forward.解决方案很简单。

use Destroy(gameobject,5);使用Destroy(gameobject,5); after u initiate that gameobject.在你启动那个游戏对象之后。

example例子

<-- line initiate gameobject -->
Destroy(gameobject,5);

which 5 is target second before gameobject will destroy.其中 5 是游戏对象销毁之前的目标秒。

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

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