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 /
  • Help Room /
avatar image
1
Question by martipello · Mar 01, 2016 at 01:24 PM · instantiatedestroygameobjects

destroying instantiated groups of objects

quick question when my player hits death he loses all his coins

     if (collision.collider.tag.ToLower() == "death" && amountOfCoins >= 1)
     {

         ContactPoint contact = collision.contacts[0];
         Vector3 pos = contact.point;
         
         for (int i = 0; i < amountOfCoins; ++i)
         {
             SpawnCoin(pos);
         }
         globals.allCoinsCollected = 0;
         allGlobals.allGlobalCoins = 0;
     }

heres that SpawnCoin method

 void SpawnCoin(Vector3 spawnPosition)
 {
         Instantiate(coinPrefab, spawnPosition, coinPrefab.transform.rotation);
 }

these coins are collectable for a few seconds and then are destroyed like this

 void DestroyAllObjects()
 {
     
     newCoins = GameObject.FindGameObjectsWithTag("newcoins");
     if (newCoins != null)
     {
         for (var i = 0; i < newCoins.Length; i++)
         {
             Destroy(newCoins[i]);
         }
     }
 }

my problem is if you hit death, the player loses all his coins, these are dispersed around the level, and coins are collectable for 5 seconds, now say you hit death again within this 5 seconds a new group of coins are instantiated but they are all destroyed together by my DestroyAllObjects method, so whats the best way around this so that each group of coins instantiated are destroyed according to there own 5 seconds, i was thinking of either putting my destroy method inside a co routine inside my spawncoin method or about adding a new tag to my coinPrefab and adding 1 to the tag for each group of instantiated coins, im kinda a noob at this though so any food for thought on this would be appreciated. Many thanks for reading. Any and all suggestions welcome

Comment
Add comment · Show 4
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 savlon · Mar 01, 2016 at 01:54 PM 1
Share

Ins$$anonymous$$d of destroying all of your coins with a method, why not set a timer for them to be destroyed when you instantiate them? Destroy (newlyInstantiatedCoin, 5f); The Destroy method takes in a gameobject and also has the ability to take in a float for the amount of time in seconds you wish to be until it is destroyed. You will have to store an instance of your newly created coin and then use that instance to call the destroy method as I just showed you.

avatar image martipello · Mar 01, 2016 at 02:09 PM 0
Share

Thank you of course this is the answer can you add this as an answer so I can accept it @savlon

avatar image savlon martipello · Mar 01, 2016 at 02:16 PM 2
Share

Good work :) You should accept @Bunny83's answer as he has put in more work than I would. I also think future viewers will benefit more from his than my boring answer :) Good luck!

avatar image martipello savlon · Mar 01, 2016 at 02:49 PM 1
Share

no worries thank again

1 Reply

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

Answer by Bunny83 · Mar 01, 2016 at 02:08 PM

Like @savlon said above you might want to queue the coins for autodestruct at the time you instantiate them:

 void SpawnCoin(Vector3 spawnPosition)
 {
     var instance = Instantiate(coinPrefab, spawnPosition, coinPrefab.transform.rotation);
     Destroy(instance.gameObject, 5f);
 }

Other ways would be to give each coin a script that destroys the object it's attached to automatically

 public class AutoDestruct : MonoBehaviour
 {
     public float delay = 5f;
     void Start()
     {
         Destroy(gameObject, 5f);
     }
 }

You can setup the time on the coin prefab itself be changing the "delay" variable in the inspector. Each coin will handle it's removing automatically.

Since you always have packs of coins you can also group them in one gameobject and put an AutoDestruct instance on the group instead:

      // [ ... ]
      Transform group = (new GameObject("coinGroup")).transform;
      AutoDestruct script = group.gameObject.AddComponent<AutoDestruct>();
      script.delay = 5f;
      for (int i = 0; i < amountOfCoins; ++i)
      {
          Transform inst = SpawnCoin(pos);
          inst.parent = group;
      }
      // [ ...]
 
 Transform SpawnCoin(Vector3 spawnPosition)
 {
     return Instantiate(coinPrefab, spawnPosition, coinPrefab.transform.rotation). transform;
 }

This will put all coins into an empty gameobject and we manually put the AutoDestruct script onto the group so there's less overhead since not every coin needs a script, just every coin group.

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 Bunny83 · Mar 01, 2016 at 02:10 PM 0
Share

ps:
In general you might want to avoid using methods like "FindGameObjectsWithTag" if it can be avoided. Since you instantiate the objects manually you can simply keep track of them ins$$anonymous$$d of instantiate them, forget about them and then ask the engine to search for them again.

avatar image martipello · Mar 01, 2016 at 02:20 PM 0
Share

yeah thank you very much this is definitely the best way to do it, and i do know that "FindGameObjectsWithTag" is quite taxing on the program ill be using your first answer thank you very much

avatar image martipello · Mar 01, 2016 at 02:48 PM 0
Share

after trying to implement this i came across an issue with making each one an instance and destroying it so i used your second approach which is working perfectly thank you for the thoroughness of your answer

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

43 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

Related Questions

Issue iterating through gameObjects deleting and addding some 0 Answers

Problem with Destroy function with instantiated Prefabs 1 Answer

Add Clones of GameObject to List(array) 0 Answers

Instantiate() and Destroy() vs setActive() 1 Answer

When instantiating 3 clones are created rather then 1 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