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 Cervelx · Apr 02, 2017 at 12:53 PM · scrpting

Destroy GameObjects with Tag ( Not found)

Hi guys ! I have created this script to instantiate two set of coins in alternation, but the coins are not destroyed ... all the coins have the tag "Pick Up", but the script does not work to destroy them, something wrong? The result is that the sets do not alternate and overlap continuously

     public GameObject coin;
 public GameObject coinB;
 public Transform coinPosition;
 public float spawnTime = 40f;
 public float spawnTimeB = 20f;
 public float destroyTime = 20f;

     void Start () 
 {
     startTime = Time.time;
     InvokeRepeating ("Spawn", spawnTime, spawnTime);
     InvokeRepeating ("SpawnB", spawnTimeB, spawnTimeB);
     InvokeRepeating ("destroyCoin", destroyTime, destroyTime);
 }

      void Spawn ()
 {
     Instantiate (coin, coinPosition.position, coinPosition.rotation);
 }

 void SpawnB ()
 {
     Instantiate (coinB, coinPosition.position, coinPosition.rotation);
 }

 void destroyCoin()
 {
     Destroy(GameObject.FindWithTag("Pick Up"));
 }
Comment
Add comment · Show 3
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 Alexander-Seeck · Apr 02, 2017 at 01:03 PM 0
Share

GameObject.FindWithTag returns the first gameobject with that tag it can find. There's no deter$$anonymous$$istic order in that. If you want to destroy specific coins in order, you can use a Queue, which removes the first element when dequeue is called on it.

avatar image Cervelx Alexander-Seeck · Apr 02, 2017 at 01:53 PM 0
Share

no, i want destroy ALL game objects at the same time, then i spawn another set of coins, there is a way for this ?

avatar image salamander555 Cervelx · Apr 02, 2017 at 02:20 PM 0
Share

then jsut do this Foreach (GameObject coin in GameObject.FindWithTag("Pick Up") { Destroy (coin) } what this does is It find all gameobjects with the tag and runs this piece of code for all of them

1 Reply

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

Answer by salamander555 · Apr 02, 2017 at 01:59 PM

First of all I think it would be good if you made the event public

 public void ...

second, are you looking for all the coins to destroy or only for 1 specific one if you are looking for all the coins you can jsut do this

 Foreach (GameObject coin in GameObject.FindWithTag("Pick Up")
 {
 Destroy (coin)
 }

else I would make sure that the coin you are looking for has a thing different from the others

 Foreach (GameObject coin in GameObject.FindWithTag("Pick Up")
 {
 if ('certain statement')
 {
 Destroy (coin)
 }
 }

Or what I think is the best solution for you make the event more specific so that not only an event is triggered but that a variable is send together with it

 void destroyCoin (GameObject coin)
 {
 Destroy (coin)
 }

if you use this make sure that when the event is triggerd that there is something like this

 if ('statement')
 {
 GameObject.Find("").Getcomponent<'script'>.destroycoin(gameobject)
 }

any question about what standing here/ what does this functions do jsut ask

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 Cervelx · Apr 02, 2017 at 03:36 PM 0
Share

i try this but not work :(, i want destroy all game object with tag "Pick Up" at the same time :)

    public void destroyCoin()
 {
     foreach (GameObject coin in GameObject.FindWithTag("Pick Up")
     {
         Destroy(coin);
     }
 }
avatar image salamander555 Cervelx · Apr 02, 2017 at 03:42 PM 0
Share

yeah my bad its not "FindwithTag" it search for something like "FindallobjectsWithtag"

avatar image Cervelx salamander555 · Apr 02, 2017 at 03:47 PM 1
Share

YEA IT WOR$$anonymous$$ THAN$$anonymous$$ YOU !

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

66 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

Related Questions

Stop Coroutine itself? 2 Answers

C# library to detect the position of physical objects on the screen 1 Answer

Field Initializer Error 1 Answer

Scripts don't execute at all. 0 Answers

A question about references in C# 2 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