简体   繁体   English

如何防止在Unity中销毁对象的所有实例?

[英]How do I prevent the destroying of all instances of an object in Unity?

I have a prefab object called Beam, which contains several things but one is an object that when an instance of it is collided with and triggered, should destroy itself. 我有一个名为Beam的预制对象,它包含几个东西,但有一个是一个对象,当它的一个实例碰撞并被触发时,它应该自我毁灭。

Currently, I have the script that generates all of the instances on a variable called Beams. 目前,我有一个脚本可以生成一个名为Beams的变量的所有实例。 Shown here: 如图所示:

在此输入图像描述

When that runs, it creates clones within it. 当它运行时,它会在其中创建克隆。 Seen here: 见到这里:

在此输入图像描述

You will also see in the last image, the Beam prefab that contains the Cookie in it. 您还将在最后一张图片中看到包含Cookie的Beam预制件。 That cookie is where I have a script that says, if I hit it, destroy. 那个cookie就是我有一个脚本,说如果我点击它就会破坏。 That code looks like this: 该代码如下所示:

...

public class Collectibles : MonoBehaviour
{
    GameManager game;

    // Start is called before the first frame update
    void Start()
    {
        game = FindObjectOfType<GameManager> ();
    }

    ...

    void OnTriggerEnter2D(Collider2D other) {
        if(other.tag == "Player"){
            string coinType = "Cookie";
            game.AddCollectible(coinType);
            Destroy(gameObject);
        }


    }
}

Currently, when I run into a cookie, it runs Destroy(gameObject) and destroys ALL instances of the cookie (one per each Clone). 目前,当我遇到cookie时,它会运行Destroy(gameObject)并销毁cookie的所有实例(每个Clone一个)。

This code lives on Cookie, not on Beams. 此代码位于Cookie上,而不是Beams上。 Is that correct? 那是对的吗? Should I have the code somewhere else? 我应该把代码放在其他地方吗? I also tried Destroy(this) but that doesn't do what I thought it would do (just the instance). 我也尝试过Destroy(this),但这并不是我认为它会做的(只是实例)。

Is it possible that from where I was calling Destroy, the script doesn't have access to the instances, or am I missing something? 是否有可能从我调用Destroy的地方,脚本无法访问实例,或者我错过了什么? Thank you in advance! 先感谢您!

If I understand your question, you want that when the "player" collides with an instance of "beam" only destroy the instance of Cookie (or the gameobject that contains the script), in this case it would do so with the tag: 如果我理解你的问题,你想要当“播放器”与“beam”实例发生碰撞时只破坏Cookie的实例(或包含该脚本的游戏对象),在这种情况下,它会使用标签:

public GameObject[] arrayofcookie;

public int destroyedinstances=1; 
//this int will tell how many instances you want to be destroyed (from the last instantiated to the first)

//for this example the last instance will be deleted


public void destroyCookie()
{
    arrayofcookie= GameObject.FindGameObjectsWithTag("Cookie");
    for (int i = 0; i < destroyedinstances; i++)
    {
        Destroy(arrayofcookie[i].gameObject);
    }
}

You call this method in the cookie script, in the collider or if you prefer with an invoke method after N seconds. 您可以在cookie脚本中,在对撞机中调用此方法,或者在N秒后使用调用方法调用此方法。

I do not think I have understood your question very well, but in these problems I prefer to use the label and it also depends on the nature of your game 我认为我不太了解你的问题,但在这些问题中,我更喜欢使用标签,这也取决于游戏的性质

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

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