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 GabeGabe · Jul 03, 2015 at 12:10 AM · unity 5destroy

How can I destroy GameObjects on the screen with the same name?

I've got a prefab called "GoodBall" and there is a method what creates these gameobjects. Iwould like to destroy them with a method. So if I play on the inspector it looks like : GoodBall(Clone). How can I destroy them?

Comment
Add comment · Show 1
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 Rellfy · Jul 03, 2015 at 12:44 AM 0
Share

Try

GameObject object = GameObject.Find("GoodBall(Clone)"); Destroy(object);

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by fsdjfsdkjfsdk · Jul 03, 2015 at 07:36 AM

I would suggest using tags. Tags are a way to group similar game objects into one group. It you put a tag titled "ball" on the prefab, when it is created in the scene it will remain with that tag. You can then destroy them all using:

gameObjects = GameObject.FindGameObjectsWithTag ("ball");

  for(var i = 0 ; i < gameObjects.length ; i ++){
      Destroy(gameObjects[i]);

}

IMPORTANT here is what tagging is if you are unfamiliar with this concept. https://www.youtube.com/watch?v=EA54-vfLkUI

Comment
Add comment · 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 GabeGabe · Jul 04, 2015 at 09:13 AM 0
Share

Thanks! Yeah, I know about tagging and I used it but I wanted to a List insted of a simple array. This is how I made it and now it works:

 public void DestroyBallsOnScreen(){
         ballsOnScreen = GameObject.FindGameObjectsWithTag("Good");
         ballsOnScreen = GameObject.FindGameObjectsWithTag("Bad");
         print (ballsOnScreen.Length.ToString ());
         for(int i = 0; i < ballsOnScreen.Length; i++){
             Destroy(ballsOnScreen[i]);
         }
     }
avatar image
0

Answer by terencetan · Jul 03, 2015 at 07:36 AM

You're best bet is still on instantiation to keep an array index of these objects and Destroy those objects.

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
Wiki

Answer by starikcetin · Jul 03, 2015 at 01:06 AM

When instantiating, you can also make a referance to instantiated GameObject which can be used later on script. Like this (Three dots means you can fill there with whatever you want, as long as it's an instantiatable object.):

 GameObject go = Instantiate(...) as GameObject;

Then, you can use "go" referance to destroy the object:

 Destroy(go);

Best wishes.

Comment
Add comment · Show 4 · 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 GabeGabe · Jul 03, 2015 at 07:27 AM 0
Share

Thanks, but it's not working. Here is my code:

 public void Drop()
     {
         if (gameIsPlaying) {
             GameObject go = Instantiate (balls [Random.Range (0, balls.Length)], this.transform.position, Quaternion.identity) as GameObject;
             ballsOnScreen.Add(go);
             Invoke ("Drop", Random.Range ($$anonymous$$Time, maxTime));
         } else {
         }
     }
 
     public void DestroyBallsOnScreen(){
         print (ballsOnScreen.Count.ToString());
         foreach (GameObject ball in ballsOnScreen) {
             Destroy(ball);
         }
     }

No matter how many balls are on the screen the size of the List is 0. And I don't know why...

avatar image starikcetin · Jul 03, 2015 at 08:28 AM 0
Share

@GabeGabe so, they are instantiated but you list has 0 count, right?

avatar image GabeGabe · Jul 04, 2015 at 09:07 AM 0
Share

Yes. I saw 2 or 3 balls on the screen but the count is 0.

avatar image GabeGabe · Jul 04, 2015 at 09:14 AM 0
Share

Now I solved. This is how:

 public void DestroyBallsOnScreen(){
         ballsOnScreen = GameObject.FindGameObjectsWithTag("Good");
         ballsOnScreen = GameObject.FindGameObjectsWithTag("Bad");
         print (ballsOnScreen.Length.ToString ());
         for(int i = 0; i < ballsOnScreen.Length; i++){
             Destroy(ballsOnScreen[i]);
         }
     }

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

6 People are following this question.

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

Related Questions

Removing instantiated objects?? 1 Answer

Destroy script and component it depends on 2 Answers

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

How can I remove an object when colliding with an other object? 1 Answer

i have problem with animation play via script 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