Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Destroy an Object Instantiate

Discussion in 'Scripting' started by killtrols, May 22, 2018.

  1. killtrols

    killtrols

    Joined:
    May 22, 2018
    Posts:
    13
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ColumnPool : MonoBehaviour {
    6.  
    7.     public int columnPoolSize = 5;
    8.     public GameObject columnPrefab;
    9.  
    10.     public float columnMin = -2.9f;
    11.     public float columnMax = 1.4f;
    12.     private float spawnXPosition = 10f;
    13.  
    14.     private GameObject[] columns;
    15.     private Vector2 objectPoolPosition = new Vector2(-14, 0);
    16.  
    17.     private float timeSinceLastSpawned;
    18.     public float spawnRate;
    19.  
    20.     private int currentColumn;
    21.  
    22.     // Use this for initialization
    23.     void Start () {
    24.         columns = new GameObject[columnPoolSize];
    25.         for(int i = 0; i < columnPoolSize;i++)
    26.         {
    27.             columns[i] = Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
    28.         }
    29.         SpawnColumn();
    30.     }
    31.    
    32.     // Update is called once per frame
    33.     void Update () {
    34.         timeSinceLastSpawned += Time.deltaTime;
    35.         if(GameController.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
    36.         {
    37.             timeSinceLastSpawned = 0;
    38.             SpawnColumn();
    39.         }
    40.     }
    41.  
    42.     void SpawnColumn()
    43.     {
    44.         float spawnYPosition = Random.Range(columnMin, columnMax);
    45.         columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
    46.         currentColumn++;
    47.         if (currentColumn >= columnPoolSize)
    48.         {
    49.             currentColumn = 0;
    50.         }
    51.     }
    52.  
    53.     private void OnTriggerEnter2D(Collider2D collision)
    54.     {
    55.        
    56.         if (collision.tag == "Disparo")
    57.         {
    58.             Destroy(gameObject);          
    59.         }
    60.     }
    61. }
    62.  
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    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?
     
  3. killtrols

    killtrols

    Joined:
    May 22, 2018
    Posts:
    13
    Dont work The destroy. When i shoothing this prefab with 9 items in the game anything is destroy.

    Sorry for my bad english
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    No worries. :)

    Just to be clear here, do you mean:
    1. Nothing is destroyed?
    2. Everything is destroyed?
    If it is (2), then presumably "everything" means all objects with tag "Disparo"?
     
  5. killtrols

    killtrols

    Joined:
    May 22, 2018
    Posts:
    13
    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
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Have you tried:
    1. Create a scene with just one column prefab and one Disparo.
    2. Set a breakpoint in the debugger at line 56.
    3. Start the scene with the one Disparo moving towards the one column prefab.
    4. Do you see the breakpoint being hit?
     
  7. killtrols

    killtrols

    Joined:
    May 22, 2018
    Posts:
    13
    I dont can do breakpoint dont work, i think this is for use the phone for "play" with unity Remote 5
     
  8. killtrols

    killtrols

    Joined:
    May 22, 2018
    Posts:
    13
    I finally i do the breakpoint work but dont enter in line 56 and 53
     
  9. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, so now we need to go through a checklist of things to see why you didn't hit your breakpoint.
    1. Have you checked the Physics engine settings to see that the layers these 2 objects are on can collide?
    2. Have you checked the collision matrix to check they have the correct components?
    3. Are you using all 2D objects (i.e. a 3D collider will not hit a 2D collider)?
     
  10. killtrols

    killtrols

    Joined:
    May 22, 2018
    Posts:
    13
    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)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MovimientoBala : MonoBehaviour {
    6.  
    7.     private Rigidbody2D rb2d;
    8.     public float velocidad;
    9.  
    10.     public float tiempoVida;
    11.  
    12.  
    13.     void Awake()
    14.     {
    15.         rb2d = GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     // Use this for initialization
    19.     void Start()
    20.     {
    21.         rb2d.velocity = new Vector2(velocidad, rb2d.velocity.y);
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update () {
    26.         Destroy(gameObject, tiempoVida);
    27.     }
    28.  
    29.     private void OnTriggerEnter2D(Collider2D collision)
    30.     {
    31.         if (collision.tag == "Missile")
    32.         {
    33.             Destroy(gameObject);
    34.         }
    35.     }
    36. }
     

    Attached Files:

  11. killtrols

    killtrols

    Joined:
    May 22, 2018
    Posts:
    13
    Finally i do it!

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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MovimientoBala : MonoBehaviour {
    6.  
    7.     private Rigidbody2D rb2d;
    8.     public float velocidad;
    9.  
    10.     public float tiempoVida;
    11.  
    12.  
    13.     void Awake()
    14.     {
    15.         rb2d = GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     // Use this for initialization
    19.     void Start()
    20.     {
    21.         rb2d.velocity = new Vector2(velocidad, rb2d.velocity.y);
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update () {
    26.         Destroy(gameObject, tiempoVida);
    27.     }
    28.  
    29.     private void OnTriggerEnter2D(Collider2D collision)
    30.     {
    31.         Destroy(collision.gameObject);
    32.         Destroy(gameObject);
    33.     }
    34. }
     
  12. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Excellent! Glad you got it working. :)
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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.

    :)
     
  14. killtrols

    killtrols

    Joined:
    May 22, 2018
    Posts:
    13
    But now when i destroy, this object dont respawn more :S how can respawn again after destroy this?
     
  15. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    My mistake. I wasn't sure whether that was a general comment or the next question. :confused:

    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:
    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         Destroy(collision.gameObject); // I am assuming this is the column being destroyed?
    4.         Destroy(gameObject);
    5.     }
    With this:
    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         <<reference to ColumnPool>>.DestroyOldAddNew( collision.gameObject );
    4.         Destroy(gameObject);
    5.     }
    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. :)

    Does any of that help?
     
  16. PiterQ70

    PiterQ70

    Joined:
    Mar 3, 2015
    Posts:
    82
    Maybe don't destroy object. SetActive to false and to true when you want.
     
  17. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    That's a good point. In fact, the request to 'respawn' may just mean to move it to the 'back' of the visible columns. :)
     
  18. killtrols

    killtrols

    Joined:
    May 22, 2018
    Posts:
    13
    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
    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if(collision.tag == "Missile")
    4.         {
    5.             collision.gameObject.SetActive(false);
    6.             Destroy(gameObject);
    7.         }      
    8.     }
    In class ColumnPool
    Code (CSharp):
    1. void Update () {
    2.         timeSinceLastSpawned += Time.deltaTime;
    3.         if(GameController.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
    4.         {
    5.             timeSinceLastSpawned = 0;
    6.             SpawnColumn();
    7.         }
    8.     }
    9.  
    10.     public void SpawnColumn()
    11.     {
    12.         float spawnYPosition = Random.Range(columnMin, columnMax);
    13.         columns[currentColumn].SetActive(true);
    14.         columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);      
    15.         currentColumn++;
    16.         if (currentColumn >= columnPoolSize)
    17.         {
    18.             currentColumn = 0;
    19.         }
    20.     }
     
  19. killtrols

    killtrols

    Joined:
    May 22, 2018
    Posts:
    13
    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
     
  20. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    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. :)
     
  21. killtrols

    killtrols

    Joined:
    May 22, 2018
    Posts:
    13
    I just do it:

    Code (CSharp):
    1.  
    2. private Vector2 objectPoolPosition = new Vector2(-14, 0);
    3. public void MoveOldAddNew(GameObject columnToDestroy)
    4.     {
    5.         columnToDestroy.transform.position = objectPoolPosition;
    6.     }