Destroy an Object Instantiate

HI

I am trying to destroy an instantiate object up to 9 times (with a for () and saving it in an array). But unfortunately it does not work. I need help please.

Thank you so much for everything

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColumnPool : MonoBehaviour {

    public int columnPoolSize = 5;
    public GameObject columnPrefab;

    public float columnMin = -2.9f;
    public float columnMax = 1.4f;
    private float spawnXPosition = 10f;

    private GameObject[] columns;
    private Vector2 objectPoolPosition = new Vector2(-14, 0);

    private float timeSinceLastSpawned;
    public float spawnRate;

    private int currentColumn;

    // Use this for initialization
    void Start () {
        columns = new GameObject[columnPoolSize];
        for(int i = 0; i < columnPoolSize;i++)
        {
            columns[i] = Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
        }
        SpawnColumn();
    }
   
    // Update is called once per frame
    void Update () {
        timeSinceLastSpawned += Time.deltaTime;
        if(GameController.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
        {
            timeSinceLastSpawned = 0;
            SpawnColumn();
        }
    }

    void SpawnColumn()
    {
        float spawnYPosition = Random.Range(columnMin, columnMax);
        columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
        currentColumn++;
        if (currentColumn >= columnPoolSize)
        {
            currentColumn = 0;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
       
        if (collision.tag == "Disparo")
        {
            Destroy(gameObject);           
        }
    }
}

Hi,

So you have a for loop on line 25. And you are destroying objects on line 58. I can’t see anything that might happen specifically 9 times.

When you say it does not work- what exactly are you expecting to happen? And what is actually happening instead?

Dont work The destroy. When i shoothing this prefab with 9 items in the game anything is destroy.

Sorry for my bad english

No worries. :slight_smile:

Just to be clear here, do you mean:

  • Nothing is destroyed?
  • Everything is destroyed?

If it is (2), then presumably “everything” means all objects with tag “Disparo”?

I want destroy columnPrefab when this is collides with “Disparo”. But at the moment dont destroy nothing.
I do same with sprite “Disparo” (When this collides with “Missile”) and this sprite is destroy but the other don’t

Have you tried:

  • Create a scene with just one column prefab and one Disparo.
  • Set a breakpoint in the debugger at line 56.
  • Start the scene with the one Disparo moving towards the one column prefab.
  • Do you see the breakpoint being hit?

I dont can do breakpoint dont work, i think this is for use the phone for “play” with unity Remote 5

I finally i do the breakpoint work but dont enter in line 56 and 53

Ok, so now we need to go through a checklist of things to see why you didn’t hit your breakpoint.

  • Have you checked the Physics engine settings to see that the layers these 2 objects are on can collide?
  • Have you checked the collision matrix to check they have the correct components?
  • Are you using all 2D objects (i.e. a 3D collider will not hit a 2D collider)?

I using all 2d.
I dont know how reply your other questions so i send pic of the objects

and the code of “Disparo” (shooting)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovimientoBala : MonoBehaviour {

    private Rigidbody2D rb2d;
    public float velocidad;

    public float tiempoVida;


    void Awake()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Use this for initialization
    void Start()
    {
        rb2d.velocity = new Vector2(velocidad, rb2d.velocity.y);
    }

    // Update is called once per frame
    void Update () {
        Destroy(gameObject, tiempoVida);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Missile")
        {
            Destroy(gameObject);
        }
    }
}


Finally i do it!

I destroy finally and not respawn more. I want respawn after =S

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovimientoBala : MonoBehaviour {

    private Rigidbody2D rb2d;
    public float velocidad;

    public float tiempoVida;


    void Awake()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Use this for initialization
    void Start()
    {
        rb2d.velocity = new Vector2(velocidad, rb2d.velocity.y);
    }

    // Update is called once per frame
    void Update () {
        Destroy(gameObject, tiempoVida);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Destroy(collision.gameObject);
        Destroy(gameObject);
    }
}

Excellent! Glad you got it working. :slight_smile:

I think he wants it to respawn after. I was writing a response to this thread earlier, but thought I’d wait to see what came of your discussion with the OP.

:slight_smile:

But now when i destroy, this object dont respawn more :S how can respawn again after destroy this?

My mistake. I wasn’t sure whether that was a general comment or the next question. :face_with_spiral_eyes:

Apologies killtrols, I didn’t realise that was your next question (as methos5k pointed out above).

So if I am understanding correctly- a missile has just shot a column, both missile and column have been destroyed, and now you want to add back another column?

In your original code you posted, you have a ColumnPool class. Maybe you could add a method to that class public void DestroyOldAddNew( GameObject columnToDestroy )?

That method could find the columnToDestroy in private GameObject[ ] columns; and do the destroy there instead. Then it would instantiate a new column and add it into columns. To do this, you would replace:

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Destroy(collision.gameObject); // I am assuming this is the column being destroyed?
        Destroy(gameObject);
    }

With this:

    private void OnTriggerEnter2D(Collider2D collision)
    {
        <<reference to ColumnPool>>.DestroyOldAddNew( collision.gameObject );
        Destroy(gameObject);
    }

My only concern here, is that your ColumnPool seems to be ‘pushing’ columns ahead in the Update(). You may find that you need to reorder columns based on distance- but I’m not sure about that bit. :slight_smile:

Does any of that help?

Maybe don’t destroy object. SetActive to false and to true when you want.

That’s a good point. In fact, the request to ‘respawn’ may just mean to move it to the ‘back’ of the visible columns. :slight_smile:

I go try create and do the method DestroyOldAddNew(). I tried the SetActive and the column dont move when respwan again after of be destroyed.
In class MovementShooting

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "Missile")
        {
            collision.gameObject.SetActive(false);
            Destroy(gameObject);
        }       
    }

In class ColumnPool

void Update () {
        timeSinceLastSpawned += Time.deltaTime;
        if(GameController.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
        {
            timeSinceLastSpawned = 0;
            SpawnColumn();
        }
    }

    public void SpawnColumn()
    {
        float spawnYPosition = Random.Range(columnMin, columnMax);
        columns[currentColumn].SetActive(true);
        columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);      
        currentColumn++;
        if (currentColumn >= columnPoolSize)
        {
            currentColumn = 0;
        }
    }

I finally did something. I create the method, but i thought… i can move off the screen, and finally i do it. I move off the screen, the column disappears and is “respawn” again.
Thank you very much for everything really

Just to check my understanding - you are moving the column out of sight and then, after some time, in void SpawnColumn(), the column gets moved back into view in the distance?

Pleased to hear that you got it sorted out. :slight_smile: