Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 e-bonneville · Mar 29, 2010 at 11:44 PM · arraydestroytargetrtsiterate

Iterating through and deleting gameObjects

Hi, I'm back. This time, I was wondering how to iterate through and delete gameObjects using JS. I'm using an invisible sphere as a target for a LookAt function. It's created when the user clicks. I'll not bore you with the details, but basically, I need to iterate through a list of gameObjects tagged "Target", and delete them. This is because every time I click, it creates a new target. When a unit gets there, the target is destroyed. However, if I click twice, the unit ignores the old target and goes to the new one. This means that there is a target the unit doesn't get to, and therefor isn't destroyed. After several minutes of gameplay, there are empty targets all over the place. This isn't good, and I sought to iterate through them and delete them one by one. However, I'm not sure how to do this. I tried several different codes cobbled together from a bunch of different sources, and spent a long time trying to get it to work. Not being successful, I decided to pick the brains of the gurus, and so posted my question here again. Unfortunately, I can't upvote an answer more than once, but I'm hoping even 10 rep is an incentive to answer... Thanks in advance!

Elliot Bonneville.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tetrad · Mar 29, 2010 at 11:52 PM

http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html

That'll return a GameObject array that you can then do something like (assuming C#)

GameObject[] objects = GameObject.FindGameObjectsWithTag( "Target" );
foreach( GameObject go in objects )
{
    Destroy( go );
}
Comment
Add comment · Show 3 · 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 e-bonneville · Mar 30, 2010 at 12:02 AM 0
Share

I'm doing JS. Can you do that in JS? I know some of the broad differences between JS and C#, but not enough to translate something like that. I'll try though. I didn't know you could just destroy a gameobject. Thanks a ton!

avatar image Eric5h5 · Mar 30, 2010 at 12:18 AM 1
Share

@elbon96: Yes, you can do that in JS. for (go in objects) Destroy (go)

avatar image e-bonneville · Mar 30, 2010 at 12:30 AM 0
Share

Thanks a ton! Got it. in fact, I was placing my code in the wrong place. Thanks everyone!!

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

No one has followed this question yet.

Related Questions

Why is iterating an array and destroying objects considered bad practice? 2 Answers

destroying a unit and removing it from a list 2 Answers

How to destroy all the items with one function? 2 Answers

How to destroy the first clone of a UI Image using an array every time I press a button? 1 Answer

Destroy Particle System in an Array 0 Answers


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