TeachMeEverything
TeachMeEverything

Reputation: 158

How do I destroy an instantiated game object clone?

I am trying to check if an object appears above a number. And if it does I want to destroy it and recreate it. And if it does not I want to create it. I have tried the following code. It creates the object but does not destroy it.

GameObject newGameObject = null;
if (x > 3.14)
        {
            if (newGameObject != null && newGameObject.scene.IsValid())
            {
                Destroy(newGameObject);
                newGameObject = GameObject.Instantiate(object1);
            }
            else
            {
                newGameObject = GameObject.Instantiate(object1);
            }

        }
else
        {
             Destroy(newGameObject);
        }

When running multiple times the objects never destroy but stack and I can see them in the hierarchy not being destroyed.

I have tried giving the newGameObject a tag so that I could destroy the tagged item and it did not tag any clones.

I tried find name of gameobject + "(clone)" and destroy them but it did not let me.

I have tried adding 1f timer, destroy(newGameObject,1f) does not destroy game object.

Upvotes: 1

Views: 272

Answers (2)

Saeleas
Saeleas

Reputation: 548

If the code is all part of an OnClick callback then here is why you are not destroying any object:

The main culprit is your definition of GameObject newGameObject = null inside the method. The value of newGameObject will always be null inside of the method so the statement if (newGameObject != null && newGameObject.scene.IsValid()) will always return false because newGameObject is always null and that is why you see all those GameObjects being instantiated. The case when if(x > 3.14) fails does not destroy anything as you are trying to destroy newGameObject, which is null.

To be able to destroy the object you will need a class field to store the reference to the last GameObject spawned.

Your MonoBehaviour will then look something like this:

public class MyBehaviour : MonoBehaviour
{
    private GameObject newGameObject;

    /* other useful code */

    public void OnClick(){
        if (x > 3.14)
        {
            if (newGameObject != null && newGameObject.scene.IsValid())
            {
                Destroy(newGameObject);
                newGameObject = GameObject.Instantiate(object1);
            }
            else
            {
                newGameObject = GameObject.Instantiate(object1);
            }
        }
        else
        {
             Destroy(newGameObject);
        }
    }
}

Upvotes: 2

Siva
Siva

Reputation: 359

GameObject newGameObject = null;
if (x > 3.14)
        {
            if (newGameObject != null && newGameObject.scene.IsValid())
            {
                Destroy(newGameObject);
                newGameObject = GameObject.Instantiate(object1);
            }
            else
            {
                newGameObject = GameObject.Instantiate(object1);
            }

        }
else
        {
             Destroy(newGameObject);
        }

Try this. Everytime when you create a new object after destroy will create one new object.

Upvotes: 1

Related Questions