How to delete old clones when a new one is added

what kind of if statement strip can you guy help me with that can keep me keep a new spawn of a gameobject but delete and older one so there’s always only on gameobject of that type. and using the tag ‘player’. thanks for all the help

Instantiate returns a reference to what it makes Keep a reference to the old one, then Destroy() it.

What part of that are you having difficulty with?

am trying to see what code i can come up with. am new to coding so am a little lost with what to do.6483769--728518--upload_2020-11-2_21-55-28.png

i used the code before to destroy the clones i create on player as i spawn to a particular location but it deletes even the player character i want to spawn next such that my spawn event on button click wont even spawn any more. so am trying to make an if statement that tries to delete the old spawn of player and always maintain one player spawned deleting every old spawn of the prefab player. hope i have explained it correctly

Line 11 will destroy the first thing it finds that matches that tag.

Break it apart into two statements so you can reason about whether that is the correct thing to do:

var temp = GameObject.FindWithTag( gameObjecTag);

Debug.Log(temp);

Destroy(temp);

Now see if what prints in your log is reasonable.

Finding things by tag is not what I suggested above. I said keep a reference, which might be returned from the Instantiate call you first used.

Hi thanks for the prompt response. sorry i was just trying to show you what i was using to destroy said player clone objects. am using an asset todo the spawning cause as earlier mention am not proficient yet at coding. so when you suggest i ‘keep a reference, which might be returned from the instantiate call you first used’ am a little more than lost.in theory i know what you mean but i’ll try and research how the code would be written.

what i want to do is for a me to have a code snippet that would help me maintain only one copy of said clone no matter how many times i spawn said clone/prefab. is there an code you can suggest?

Traditionally what you do is in the code module that is responsible for spawning:

  1. see if you had a reference to a previous one, if so destroy it

  2. create a new spawn and keep a reference to it so next time you can do #1 above

thanks kurt, much appreciated. let me get on that.

1 Like

Could you please share the code you used for this subject?

I just could fix it just with one line of code. In case of somebody needs it, sharing here.

[SerializeField] private GameObject goPrefab;

private void OnEnable()
    {
        if(clone == false)   //one line of code that i added.
        {
            clone = Instantiate((goPrefab), gameObject.transform).GetComponent<PrefabName>();
            button = GetComponent<Button>();
            image = GetComponent<Image>();
        }
    }

While it is wonderful you are coming back to share nearly two full years later, could you please use code tags? Nobody is going to even look at your code the way it is posted above.

How to use code tags: https://discussions.unity.com/t/481379

You may edit your post above.

1 Like