Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to destroy all objects with tag "soandso"?

Discussion in 'Scripting' started by JohannChristoph, Sep 2, 2009.

  1. JohannChristoph

    JohannChristoph

    Joined:
    Jun 21, 2009
    Posts:
    141
    hello forum.

    i want to destroy. No i just want to destroy a number of cloned Objects which were created on runtime with Instantiate. During the creation i tag these objects with "CurvePoint".
    I need to destroy those tagged clones by calling the function DestroyClones () during runtime. I tried to make it kinda like in the Script Reference under 'GameObject.FindGameObjectsWithTag' only with destroy. But i am missing something.


    Code (csharp):
    1. static function DestroyClones () {
    2.    
    3.     var clones = GameObject.FindGameObjectsWithTag ("CurvePoint");
    4.    
    5.     for (var i in clones) {
    6.    
    7.         //   this creates an error message
    8.         Destroy (clones);
    9.        
    10.         //   this prints 'hello' i times but destroys only one clone
    11.         //print("hello");
    12.         //var aClone = GameObject.FindWithTag ("CurvePoint");
    13.         //Destroy (aClone);    
    14.        
    15.     }
    16. }
    basicly i want to destroy all objects with the tag "CurvePoint".
     
  2. Sam at FPS

    Sam at FPS

    Joined:
    Sep 1, 2009
    Posts:
    80
    What error message does it produce?

    If it was C# I would say that it's because you can't destroy objects that you are iterating through using a foreach loop.

    Try using a normal for loop.
    Code (csharp):
    1. for (var i=0; i < array.length; i++)
    2. {
    3.    Destroy(array[i]);
    4. }
    5.  
     
  3. JohannChristoph

    JohannChristoph

    Joined:
    Jun 21, 2009
    Posts:
    141
    it is a JavaScript. the error on just Destroy (clones); is:

    BCE0023: No appropriate version of 'UnityEngine.Object.Destroy' for the argument list '((UnityEngine.GameObject))' was found.

    thanx so far!
    I try a normal for loop.
     
  4. Sam at FPS

    Sam at FPS

    Joined:
    Sep 1, 2009
    Posts:
    80
    OK, your code should be

    Code (csharp):
    1. Destroy(i)
    not

    Code (csharp):
    1. Destroy(clones)
    shouldn't it? You can't destroy the object you are iterating through.
     
  5. JohannChristoph

    JohannChristoph

    Joined:
    Jun 21, 2009
    Posts:
    141
    oh. Yes!
    everything works perfect now.
    Thank you very much! :D