- Home /
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.
Answer by whydoidoit · Jun 17, 2012 at 10:23 AM
function DestroyAllObjectsWithTag(tag : String) {
var scan : GameObject;
for(scan in GameObject.FindGameObjectsWithTag(tag)) {
GameObject.Destroy(scan);
}
}
Answer by sep05en · Jun 17, 2012 at 10:48 AM
Got error. It said, "not compatible with the argument list '()'."
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.
I've modified it and added a GameObject.Destroy() - not sure if that was it - on which line do you get the error?
Answer by reptilebeats · Jun 17, 2012 at 12:00 PM
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;
}