• 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
Question by TGamingStudio · Jul 09, 2017 at 11:57 AM · destroy

Destroy all objects with same name

I want destroy all objects with name "food(clone)" when i click Escape but it destroyed only one of that ...

 Destroy(GameObject.Find("food(clone)"));


Comment

People who like this

0 Show 0
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
Best Answer

Answer by TheDJBuntin · Jul 09, 2017 at 01:25 PM

Have the game object created with a tag. Then in your code create an array of game objects using = GameObject.FindGameObjectsWithTag. Then have a foreach loop that destroys all members of that array.

ie.

 GameObject[] foods;
 
 foods = GameObject.FindGameObjectsWithTag("Food");
 
 foreach(GameObject food in foods)
 {
     Destroy(food);
 }


Comment
SlimBun

People who like this

1 Show 1 · 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 TGamingStudio · Jul 09, 2017 at 02:47 PM 0
Share

thank you it works

avatar image

Answer by mugiwaraUnity · Aug 20, 2017 at 09:36 PM

i t$$anonymous$$nk it would be better for you to keep a reference of the object you instantiated, maybe in a list. and when you want to destroy them, you just loop into your list and destroy everyt$$anonymous$$ng then clear your list. Somet$$anonymous$$ng like t$$anonymous$$s

 List<gameobject> allTheFood = new List<gameobject>();
 
 private void CreateFood()
 {
     for(int i = 0; i < 10; i++)
     {
         allTheFood.add(Instantiate(foodprefab) as gameobject;);
     }
 }
 
 private void DestroyFood()
 {
     for(int i = 0; i <  allTheFood.count; i++)
     {
         Destroy(allTheFood[i]);
     }
     allTheFood.clear();
 }

Comment
DarkShadowsX5
Riiich

People who like this

2 Show 0 · 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

Answer by OLGV · Aug 20, 2017 at 09:26 PM

@TheDJBuntin doesn't seem to work on mobile :\ ,though it works on PC

Comment

People who like this

0 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 TheDJBuntin · Aug 20, 2017 at 09:55 PM 0
Share

Sorry I don't do mobile development and not in a position to look it up currently. Im sure unless the issue you are having is unrelated others will have this problem, have a search on Google might have some luck. Wish I could be of more help, sorry.

Ps. You posted this as an ANSWER to the Question. For future reference if you want to discuss something about a specific answer click the "Add Comment" button below that answer and then you can type your reply like how Ive done here.

avatar image OLGV · Aug 20, 2017 at 09:59 PM 0
Share

Thank you and no worries. Your previous help is much appreciated. At the moment I'm giving a try to @mugiwaraUnity 's suggestion and will update the forum once I get this working.

avatar image LuciusDesign OLGV · May 06, 2020 at 12:13 AM 0
Share

Sorry about replying in such an old post, do you still have trouble on mobile? I'm not catable of test it at the moment and I'm interested in knowing if you managed to fix it and if you did it on mobile too. thanks

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

74 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How do i destroy a character and have a particle effect after? 3 Answers

Checking if a component has been removed or not at runtime 1 Answer

Destroying the floor 2 Answers

Destroy at collision based on color 3 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