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. Dismiss Notice

Destroy an object after animation.

Discussion in '2D' started by Misak, Aug 29, 2016.

  1. Misak

    Misak

    Joined:
    Apr 8, 2013
    Posts:
    9
    Hi, like the tittle says, i want to destroy an object after it's animation. The problem is that i already did that and it works, but the thing is that i want to destroy the new clone created that i call with a InvokeRepeating and it destroy the main object and stop all the next objects from appearing.
    Or when i make that the objects destroy themselves after animation when i click on a new object with

    Destroy(this.gameObject); it happens the same.

    What i need is both things, that the new objects destroy themselves after animation and if i click in one object, destroy that object and that keep appearing objects in the scene. Idk if i'm clear.

    If u want guys i can paste my code.
    Sory for my bad english.

    Thanks you!
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Yes, please put your code up using code tags.
     
  3. jamius19

    jamius19

    Joined:
    Mar 30, 2015
    Posts:
    96
    Can't understand what you're trying to say.

    Some code will be nice!
    Do not forget to use code tags as @Hyblademin already said! :)
     
  4. franciscochong

    franciscochong

    Joined:
    Jul 9, 2015
    Posts:
    30
    Hi you shouldn't destroy objects that will be reused.
    I found out the hard way. Now to your question, have you tried Animation Events? and then add a click/touch Input for the destroying/disabling function? and not use InvokeRepeating?
     
  5. Misak

    Misak

    Joined:
    Apr 8, 2013
    Posts:
    9
    Hi, i read that using object pooling is better than InvokeRepeating but idk exactly how to do it. I'm stuck :S

    I want that every X seconds the numbers of objects in screen grows and also if i click on an object it dissapear and gives me one point and if i don't click the object when it finish it's animation it will rest me one point.

    Here is the code and even that it have no errors i know that it's not good at all this code, but here it is. I have this attached to my gameobject.

    Thank you guys for take your time and answer me!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class Burbujas : MonoBehaviour {
    7.  
    8.    
    9.     public float radius;
    10.     public float spawnTime = 3f;
    11.     public GameObject nuevaburbuja;
    12.  
    13.  
    14.  
    15.  
    16.  
    17.     private SpriteRenderer spriteRenderer;
    18.     private CircleCollider2D circleCollider;
    19.     private Sprite lastSprite;
    20.  
    21.  
    22.  
    23.  
    24.  
    25.     void OnMouseDown()
    26.     {
    27.  
    28.  
    29.         //this.gameObject.SetActive(false);
    30.         Destroy(this.gameObject);
    31.         //InvokeRepeating("SpawnObject", 2, 1.5f);
    32.  
    33.         Debug.Log("Ganaste un punto pa");
    34.  
    35. // Suma un punto
    36.      }
    37.  
    38.  
    39.     private void Start()
    40.     {
    41.  
    42.      
    43.  
    44.         circleCollider = GetComponent<CircleCollider2D>();
    45.         spriteRenderer = GetComponent<SpriteRenderer>();
    46.         UpdateColliderSize();
    47.         InvokeRepeating("SpawnObject", 2, 1.5f);
    48.         //InvokeRepeating("SpawnObjectdos", 2, 1.5f);
    49.         //InvokeRepeating("SpawnObjecttres", 5, 2f);
    50.         //InvokeRepeating("SpawnObjectcuatro", 8, 4f);
    51.  
    52.     }
    53.  
    54.  
    55. //void Start()
    56.     //{
    57.  
    58.  
    59.  
    60.        
    61.      
    62.         //Burbuja();
    63.         //InvokeRepeating(gameObject.tag = "burbuja", spawnTime, spawnTime);
    64.        
    65.      
    66.  
    67.  
    68.  
    69.     //}
    70.  
    71.    
    72.  
    73.    
    74.  
    75.  
    76.         private void Update()
    77.     {
    78.         if (spriteRenderer.sprite != lastSprite)
    79.         {
    80.             UpdateColliderSize();
    81.  
    82.          
    83.  
    84.  
    85.         }
    86.  
    87.         //if (spriteRenderer.sprite.name == ("spriteburbuja_7"))
    88.  
    89.         //{
    90.  
    91.             //Destroy(this.gameObject);
    92.             //Debug.Log("Un punto menos");
    93.  
    94.         //}
    95.  
    96.     }
    97.  
    98.     /// <summary>
    99.     ///
    100.     /// </summary>
    101.     private void UpdateColliderSize()
    102.     {
    103.         Vector3 spriteHalfSize = spriteRenderer.sprite.bounds.extents;
    104.         circleCollider.radius = spriteHalfSize.x > spriteHalfSize.y ? spriteHalfSize.x : spriteHalfSize.y;
    105.         lastSprite = spriteRenderer.sprite;
    106.        
    107.         //radio = Sprite.bounds.extents;
    108.         //Debug.Log(Sprite.bounds);
    109.         //if (circleCollider.radius > 2.97)
    110.         //{
    111.  
    112.         //Destroy(this.gameObject);
    113.         //Debug.Log("Perdiste un punto!");
    114.         //}
    115.     }
    116.  
    117.     //void Burbuja()
    118.     //{
    119.     //CircleCollider2D myCollider = transform.GetComponent<CircleCollider2D>();
    120.  
    121.      //myCollider.radius = myCollider.radius + 0.00975f; // or whatever radius you want.
    122.  
    123.     //GetComponent<CircleCollider2D>().radius = Time.time + 1.85f;
    124.  
    125.     //if (myCollider.radius > 2.96)
    126.  
    127.     //{
    128.  
    129.  
    130.     //Destroy(this.gameObject);
    131.  
    132.     //}
    133.  
    134.  
    135.     //private void Destruitobjeto;
    136.  
    137.  
    138.  
    139.     void SpawnObject()
    140.     {
    141.         float x = Random.Range(-7f, 4f);
    142.         float y = Random.Range(1f, 5f);
    143.         Instantiate(nuevaburbuja, new Vector3(x, y, 0.0f), Quaternion.identity);
    144.         Destroy(gameObject, 1);
    145.      
    146.  
    147.     }
    148.  
    149.     void SpawnObjectdos()
    150.     {
    151.         float x = Random.Range(-7f, 4f);
    152.         float y = Random.Range(1f, 5f);
    153.         Instantiate(nuevaburbuja, new Vector3(x, y, 0.0f), Quaternion.identity);
    154.      
    155.     }
    156.  
    157.     void SpawnObjecttres()
    158.     {
    159.         float x = Random.Range(-7f, 4f);
    160.         float y = Random.Range(1f, 5f);
    161.         Instantiate(nuevaburbuja, new Vector3(x, y, 0.0f), Quaternion.identity);
    162.     }
    163.  
    164.     void SpawnObjectcuatro()
    165.     {
    166.         float x = Random.Range(-7f, 4f);
    167.         float y = Random.Range(1f, 5f);
    168.         Instantiate(nuevaburbuja, new Vector3(x, y, 0.0f), Quaternion.identity);
    169.     }
    170.  
    171.  
    172.    
    173.  
    174. }
     
  6. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Create a ObjectPool Controller something like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class ObjectPool : MonoBehaviour {
    5.  
    6.     public GameObject Prefab;
    7.     List<GameObject> availableObjects = new List<GameObject>();
    8.  
    9.     GameObject GetNewObject()
    10.     {
    11.         GameObject returnObject;
    12.         if (availableObjects.Count == 0)
    13.             CreateNewObject();
    14.  
    15.         returnObject = availableObjects[0];
    16.         returnObject.SetActive(true);
    17.         availableObjects.Remove(returnObject);
    18.         return returnObject;
    19.     }
    20.  
    21.     void DestroyObject(GameObject destroyObject)
    22.     {
    23.         destroyObject.SetActive(false);
    24.         availableObjects.Add(destroyObject);
    25.     }
    26.  
    27.     CreateNewObject()
    28.     {
    29.         GameObject newObject = (GameObject)Instantiate(Prefab);
    30.         availableObjects.Add(newObject);
    31.     }
    32. }
    Then your other code that Instantiate an object instead calls:
    ObjectPool.GetNewObject();
    and Instead of Destroy
    ObjectPool.DestroyObject(someGameObject);

    This will create objects as you need, and if you destroy them it instead just keeps track of them and hands them back out next time someone needs one.

    Your code would then change the position/rotation of any object you got from the ObjectPool.
     
  7. Misak

    Misak

    Joined:
    Apr 8, 2013
    Posts:
    9
    Sory but idk where exactly put: ObjectPool.GetNewObject();
    I tried in a few places but i just can't fit it.

    I delete my InvokeRepeating and change it for:
    ObjectPool.GetNewObject() at the Start but it gives me this error:
    an object reference is required to access non-static field,method, or property 'ObjectPool.GetNewObject()


    Btw, the destroy it works fine.
     
    Last edited: Sep 2, 2016
  8. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Right here change
    Code (CSharp):
    1.  void SpawnObjectdos()
    2.     {
    3.         float x = Random.Range(-7f, 4f);
    4.         float y = Random.Range(1f, 5f);
    5.         Instantiate(nuevaburbuja, new Vector3(x, y, 0.0f), Quaternion.identity);
    6.    
    7.     }
    To:
    Code (CSharp):
    1.  void SpawnObjectdos()
    2.     {
    3.         float x = Random.Range(-7f, 4f);
    4.         float y = Random.Range(1f, 5f);
    5.         GameObject newObject = ObjectPool.GetNewObject();
    6.         newObject.transform.position = new Vector3(x,y,0.0f);
    7.            
    8.     }
    You have to change all the places where you use to Instantiate to the new ObjectPool.GetNewObject();
     
  9. Misak

    Misak

    Joined:
    Apr 8, 2013
    Posts:
    9
    I still got:


    An object reference is required to access non-static member `ObjectPool.GetNewObject()

    :S
     
  10. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Sorry ObjectPool is the class you have to make an instance of it. Make an Empty Game Object in your scene. Call it what you like (ObjectPool for example). Drag the ObjectPool script onto it. Then in Burbujas make a variable:
    public ObjectPool objectPool;
    In the Editor Drag ObjectPool GameObject onto that variable.

    Now in your script you use objectPool.GetNewObject (notice lowercase o, thats the variable name we chose above).
     
  11. Misak

    Misak

    Joined:
    Apr 8, 2013
    Posts:
    9
    Same error. :S

    Idk what else to do, i give you my code and a screenshots.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4.  
    5.  
    6. public class Burbujas : MonoBehaviour {
    7.  
    8.    
    9.     public float radius;
    10.     public float spawnTime = 3f;
    11.     public GameObject nuevaburbuja;
    12.     public ObjectPool burbujaultima;
    13.  
    14.  
    15.  
    16.  
    17.     private SpriteRenderer spriteRenderer;
    18.     private CircleCollider2D circleCollider;
    19.     private Sprite lastSprite;
    20.  
    21.  
    22.  
    23.  
    24.  
    25.     void OnMouseDown()
    26.     {
    27.  
    28.  
    29.         //this.gameObject.SetActive(false);
    30.         //Destroy(this.gameObject);
    31.         //InvokeRepeating("SpawnObject", 2, 1.5f);
    32.         ObjectPool.DestroyObject(nuevaburbuja);
    33.         Debug.Log("Ganaste un punto pa");
    34.  
    35. // Suma un punto
    36.      }
    37.  
    38.  
    39.    private void Start()
    40.     {
    41.  
    42.      
    43.  
    44.         circleCollider = GetComponent<CircleCollider2D>();
    45.         spriteRenderer = GetComponent<SpriteRenderer>();
    46.         UpdateColliderSize();
    47.         SpawnObject();
    48.         //InvokeRepeating("SpawnObject", 2, 6f);
    49.         //InvokeRepeating("SpawnObjectdos", 2, 1f);
    50.         //InvokeRepeating("SpawnObjecttres", 5, 2f);
    51.         //InvokeRepeating("SpawnObjectcuatro", 8, 4f);
    52.  
    53.     }
    54.  
    55.  
    56.     //void Start()
    57.     //{
    58.  
    59.  
    60.  
    61.  
    62.  
    63.     //Burbuja();
    64.     //InvokeRepeating(gameObject.tag = "burbuja", spawnTime, spawnTime);
    65.  
    66.  
    67.  
    68.  
    69.  
    70.     //}
    71.  
    72.  
    73.  
    74.  
    75.  
    76.  
    77.     private void Update()
    78.     {
    79.         if (spriteRenderer.sprite != lastSprite)
    80.         {
    81.             UpdateColliderSize();
    82.  
    83.          
    84.  
    85.  
    86.         }
    87.  
    88.         //if (spriteRenderer.sprite.name == ("spriteburbuja_7"))
    89.  
    90.         //{
    91.  
    92.             //Destroy(this.gameObject);
    93.             //Debug.Log("Un punto menos");
    94.  
    95.         //}
    96.  
    97.     }
    98.  
    99.     /// <summary>
    100.     ///
    101.     /// </summary>
    102.     private void UpdateColliderSize()
    103.     {
    104.         Vector3 spriteHalfSize = spriteRenderer.sprite.bounds.extents;
    105.         circleCollider.radius = spriteHalfSize.x > spriteHalfSize.y ? spriteHalfSize.x : spriteHalfSize.y;
    106.         lastSprite = spriteRenderer.sprite;
    107.        
    108.         //radio = Sprite.bounds.extents;
    109.         //Debug.Log(Sprite.bounds);
    110.         //if (circleCollider.radius > 2.97)
    111.         //{
    112.  
    113.         //Destroy(this.gameObject);
    114.         //Debug.Log("Perdiste un punto!");
    115.         //}
    116.     }
    117.  
    118.     //void Burbuja()
    119.     //{
    120.     //CircleCollider2D myCollider = transform.GetComponent<CircleCollider2D>();
    121.  
    122.     //myCollider.radius = myCollider.radius + 0.00975f; // or whatever radius you want.
    123.  
    124.     //GetComponent<CircleCollider2D>().radius = Time.time + 1.85f;
    125.  
    126.     //if (myCollider.radius > 2.96)
    127.  
    128.     //{
    129.  
    130.  
    131.     //Destroy(this.gameObject);
    132.  
    133.     //}
    134.  
    135.  
    136.     //private void Destruitobjeto;
    137.  
    138.  
    139.  
    140.     void SpawnObject()
    141.     {
    142.         float x = Random.Range(-7f, 4f);
    143.         float y = Random.Range(1f, 5f);
    144.         GameObject newObject = ObjectPool.GetNewObject();
    145.         newObject.transform.position = new Vector3(x, y, 0.0f);
    146.  
    147.     }
    148.  
    149.     void SpawnObjectdos()
    150.     {
    151.         float x = Random.Range(-7f, 4f);
    152.         float y = Random.Range(1f, 5f);
    153.         Instantiate(nuevaburbuja, new Vector3(x, y, 0.0f), Quaternion.identity);
    154.      
    155.     }
    156.  
    157.     void SpawnObjecttres()
    158.     {
    159.         float x = Random.Range(-7f, 4f);
    160.         float y = Random.Range(1f, 5f);
    161.         Instantiate(nuevaburbuja, new Vector3(x, y, 0.0f), Quaternion.identity);
    162.     }
    163.  
    164.     void SpawnObjectcuatro()
    165.     {
    166.         float x = Random.Range(-7f, 4f);
    167.         float y = Random.Range(1f, 5f);
    168.         Instantiate(nuevaburbuja, new Vector3(x, y, 0.0f), Quaternion.identity);
    169.     }
    170.  
    171.  
    172.    
    173.  
    174. }




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class ObjectPool : MonoBehaviour
    5.    
    6. {
    7.  
    8.    
    9.     public GameObject Prefab;
    10.     List<GameObject> availableObjects = new List<GameObject>();
    11.  
    12.    public GameObject GetNewObject()
    13.     {
    14.         GameObject returnObject;
    15.         if (availableObjects.Count == 0)
    16.             CreateNewObject();
    17.  
    18.         returnObject = availableObjects[0];
    19.         returnObject.SetActive(true);
    20.         availableObjects.Remove(returnObject);
    21.         return returnObject;
    22.     }
    23.  
    24.     void DestroyObject(GameObject destroyObject)
    25.     {
    26.         destroyObject.SetActive(false);
    27.         availableObjects.Add(destroyObject);
    28.     }
    29.  
    30.    void CreateNewObject()
    31.     {
    32.         GameObject newObject = (GameObject)Instantiate(Prefab);
    33.         availableObjects.Add(newObject);
    34.     }
    35. }
    Sory for being so dumb, i still try to fix that error. Thank you for your effort, i really appreciated. If u can add me on facebook i will be so happy.
    Mine is: https://www.facebook.com/misak.kalaycioglu

    Thank you very much!
     

    Attached Files:

  12. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Code (CSharp):
    1. public ObjectPool burbujaultima;
    ObjectPool is the variable type. Like float, string, int. burbujaultima is your variable name. Use this!
    Code (CSharp):
    1.  GameObject newObject = burbujaultima.GetNewObject();
     
  13. Misak

    Misak

    Joined:
    Apr 8, 2013
    Posts:
    9
    I still got this error on this line and in one more: GameObject newObject = burbujaultima.GetNewObject();
     

    Attached Files:

    Last edited: Sep 2, 2016
  14. franciscochong

    franciscochong

    Joined:
    Jul 9, 2015
    Posts:
    30
    Hi @Misak here is a very simple object pooling code.


    Just follow it, outside of your code and do the tutorial in a new scene or project.
    Understand how it works and implement as needed in your game.
    Basically you create a "pool" or "inactive" objects and add it to a Generic list.
    When you need one you check the pool (for loop on the list) for "!gameObject.activeInHierarchy".
    Make sure to create enough objects so you dont run out, this means create what you think will be the max number of said objects that will be shown at the same time. so if you foresee that you might have 50 or less at the same time, make the pool that size.

    In the tutorial it also teaches you how to make the pool "growable"

    Hope this helps!

    Happy coding
     
    Misak likes this.