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);
}
}
}
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
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);
}
}
}
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);
}
}
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.
My mistake. I wasn’t sure whether that was a general comment or the next question.
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);
}
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.
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
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?