简体   繁体   English

销毁预制件中的物体后,如何重新实例化该预制件?

[英]How do I re-instantiate original prefab after an object in that prefab has been destroyed?

My problem is that my Original prefab is not being instantiated with all of its components. 我的问题是我的原始预制件没有被其所有组件实例化。 This is because after certain events, those objects get destroyed. 这是因为在某些事件之后,这些对象将被销毁。 But When I instantiate the prefab I want it to have all of its objects again. 但是,当我实例化预制件时,我希望它再次具有其所有对象。 Does anyone know what I can do about this? 有谁知道我能做些什么?

public void OnTriggerEnter(Collider other)
{

    if (other.gameObject.name.Contains ("Player"))
    {


        if(count == 0){
            Debug.Log("Enemy Collision");
            Instantiate(ground,new Vector3(distance, 0, 0), Quaternion.identity);
            Destroy(ground);

            distance = distance+ 40.31f;

            count=1;
        }

        else{
            Debug.Log("Enemy Collision");
            Instantiate(ground,new Vector3(distance, 0, 0), Quaternion.identity);
            Destroy(ground);

            distance = distance + 40.31f;
        }

    }
}

Don't destroy the prefab -- just destroy the newly instantiated object (assuming that's your intention): 不要破坏预制件,而只是破坏新实例化的对象(假设这是您的意图):

var groundToBeDestroyed = Instantiate(ground, new Vector3(distance, 0, 0), Quaternion.identity);
Destroy(groundToBeDestroyed);

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

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