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

[Solved] Destory All GameObjects With Tag

Discussion in 'Scripting' started by GhulamJewel, Dec 24, 2014.

  1. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Hi there fairly simple question from a beginner. When my player dies I wish to destroy all objects with certain tags. Below is my script when a object collides with my player the player gets destroy. After that all enemy objects stay and respawns keep respawning objects and draining memory for no reason! So just wish to destroy objects when my player gets destroyed :)


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyCubes : MonoBehaviour
    5. {
    6.     void OnCollisionEnter (Collision col)
    7.     {
    8.         if (col.gameObject.name == "Player") {
    9.                         Destroy (col.gameObject);
    10.  
    11.  
    12.             SpecialEffectsHelperGreen.Instance.Explosion (transform.position);
    13.         }
    14.     }
    15. }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  3. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Thanks for the reply and direction. I am not sure if this modification is correct

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyCubes : MonoBehaviour
    5. {
    6.         void OnCollisionEnter (Collision col)
    7.         {
    8.                 if (col.gameObject.name == "Player") {
    9.  
    10.  
    11.                         Destroy (col.gameObject);
    12.  
    13.    
    14.                         SpecialEffectsHelperGreen.Instance.Explosion (transform.position);
    15.  
    16.                 }
    17.  
    18.         GameObject.FindGameObjectsWithTag("Enemy");
    19.  
    20.         Destroy (GameObject);
    21.  
    22.  
    23.  
    24.                 }
    25.         }
    26.  
    27. }
    28.  
    get this error

    " Assets/Scripts/DestroyCubes.cs(27,1): error CS8025: Parsing error " please advise thank you.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Parsing error simply means you have an extra } at the end of your code.

    There are other problems in this script
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    FindGameObjectsWithTag returns an array, so you have to loop through it and destroy the objects. If you check that link, in the example, they show looping through it, but they do something else, not destroy, but you'll get the idea.
     
    GhulamJewel likes this.
  6. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Thank you very much for pushing me towards the right direction and I guess not spoon feeding me haha I studied the link and some unity videos on looping and came up with this and attached it to the player and works like a charm.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroySafecube : MonoBehaviour
    5. {
    6.  
    7.         void  OnDestroy ()
    8.         {
    9.  
    10.                 GameObject[] SafeCube;
    11.    
    12.                 SafeCube = GameObject.FindGameObjectsWithTag ("Safecube");
    13.  
    14.                 foreach (var i in SafeCube)
    15.  
    16.                         Destroy (i);
    17.    
    18.         }
    19.    
    20.    
    21. }
     
    iriegusto likes this.
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Good job.