x


Destroy gameobject by tag (.js)

Hi all.

I am trying to destroy all of the objects in a scene with a certain tag, but I get error using my javascript code. Anyone can help me destroy the objects with tag using javascript code? I already declared in the objects prefab as "Model1" in tag manager. Can I declared multiple prefab w/ the same tag?

I don't know how to apply findgameobjectswithtag in this section or any better solution for it. Thank you in advance.

more ▼

asked Jun 17 '12 at 10:20 AM

sep05en gravatar image

sep05en
0 1 2 2

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first
  function DestroyAllObjectsWithTag(tag : String) {

       var scan : GameObject;
       for(scan in GameObject.FindGameObjectsWithTag(tag)) {
             GameObject.Destroy(scan);
       }

  }
more ▼

answered Jun 17 '12 at 10:23 AM

whydoidoit gravatar image

whydoidoit
33k 11 23 98

(comments are locked)
10|3000 characters needed characters left

Got error. It said, "not compatible with the argument list '()'."

more ▼

answered Jun 17 '12 at 10:48 AM

sep05en gravatar image

sep05en
0 1 2 2

Please don't use Answer to "reply", on UA "Answer" means "Solution". There is an add new comment button hidden on the right. Also your comments will not require moderation.

Jun 17 '12 at 10:49 AM whydoidoit

I've modified it and added a GameObject.Destroy() - not sure if that was it - on which line do you get the error?

Jun 17 '12 at 10:50 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left

for js it would like this

var objectTag = gameObject.FindGameObjectsWithTag("the objects tag");
for(var i : int = 0; i <objectTag.length; i++){
Destroy(objectTag[i]);

}

let me explain when accessing objects inside a tag you must create a way for it to cycle through each object, if you just state destory gameobjectwith tag then it will destroy only one, but add a simple loop then it will effect each of them.

if say you only want to delete say five of ten objects then just replace length witth a number

you could use this with other things as well for example heres a snippet from my code

var RagDollTag = gameObject.FindGameObjectsWithTag("RagDoll");
    for(var RDT : int = 0; RDT <RagDollTag.length; RDT++){
       RagDollTag[RDT].GetComponent(Rigidbody).isKinematic = false;
         RagDollTag[RDT].GetComponent(Collider).isTrigger = false;
    }
more ▼

answered Jun 17 '12 at 12:00 PM

reptilebeats gravatar image

reptilebeats
1.2k 53 104 137

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3444
x764
x321
x53

asked: Jun 17 '12 at 10:20 AM

Seen: 696 times

Last Updated: Jun 17 '12 at 12:00 PM