Instantiated clones not being destroyed

Hi guys,
Im doing some practise by copying Flappy birds.
I have the whole game set up including random placement of pipes using a collider which follows the player.

At the start of the level i have 4 prefab pipes set up whcih have names pipe 1, pipe2. After these 4 the automation kicks in and i start instantiating pipes. Which then creates prefabs with the names pipe (clone).

Part of my script is to destroy the pipe which has left the camera when it instatiates the new pipe.
What i am finding though is the script will destroy the pipes with names pipe1, pipe2 but not the objects with the word clone.

Anyone able to give me any insight into what im missing?

Thanks

CODE:

     void OnTriggerExit2D(Collider2D other)
    {
        if (other.gameObject.tag == "backdrop") {
            other.gameObject.transform.position = new Vector2 (other.gameObject.transform.position.x + 50f, other.gameObject.transform.position.y);
        }
        if (other.gameObject.tag == "Pipes") 
        {
            gamemanager.newpipe(true,other.gameObject);
        }

    }
    public void newpipe(bool pipeadd, GameObject curpipe)
    {
        if (pipeadd) {
            print (curpipe.name);
            Destroy (curpipe);
            Vector3 newPos = new Vector3 (lastPipePlacement.x + pipeplacementOffset , Random.Range (pipeYMin, pipeYMax),0f);
            //curpipe.gameObject.transform.position = newPos;
            Instantiate (pipeSprite, newPos, Quaternion.identity);
            lastPipePlacement = newPos;

        }        
    }

It’s not immediately obvious to me yet why it’s not working, but I have to ask this…
Why destroy and re-instantiate? You really could (and should, for reasons of performance- this is more of a good habit thing in this case probably, however) just move this pipe you’re planning to destroy to the new location where you were planning to instantiate the other pipe… no destroy/create… just a move. Make sense? I know it doesn’t answer why the code didn’t work right, but I think it’s a better approach.

As far as the problem with your original way of doing it, click on your clones in the heirarchy and make sure they have the proper tag, maybe?

Hey thanks for the reply.
I did switch to the move position for as you say performance reasons.
I ran into another problem when i did that so just left at instantiate and destroy for now, so i could ‘finish’ the basics of the game.
Will definetly revist though.
Just checked my prefab and clones and sadly they are the correct tag.

Strange thing is im sure this was working earlier as i only had 4 clones on the screen at all times…So not really sure what ive done to it.

OK, no problem… a couple of questions:
What is your OnTriggerExit2D attached to? (some off screen collider?)
Do you see your clones printed out from that print statement in your newpipe function?

I’d also suggest comparing your prefab that your clone is based off of to pipe1 / pipe2 to see if there’s something different (ex., missing the collider)

Think i may have figured it out.
My prefab consits on two spries each with box colliders that are both flagged as pipes.
Looks like when the hidden collider of screen collides it is running the instantiate function twice. Giving the impression that the clones arnt being deleted.

Ahh, that would do it. :slight_smile:

1 Like

Looks like thats all it was. Switched my two sprites into an empty gameobject and tagged that.
Then switched my code to look at the Parent tag.
Another issue bites the dust.
Thanks for your help.

1 Like

No problem, glad to see you figured it out!!