Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sep05en · Jun 17, 2012 at 10:20 AM · javascriptdestroytagfindgameobjectswithtag

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
        }
     
   }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by sep05en · Jun 17, 2012 at 10:48 AM

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Jun 17, 2012 at 10:49 AM 0
Share

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.

avatar image whydoidoit · Jun 17, 2012 at 10:50 AM 0
Share

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

avatar image
0

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;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

strange behaviour when creating and deleting objects with tags 1 Answer

On Click, destroy object and any colliding object with the same tag. 6 Answers

Find child of a Game Object using tag 4 Answers

Player tag not being detected by Unity? 2 Answers

Script destroys all gameobjects with script. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges