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
1
Question by SSpecks · Jan 09, 2020 at 07:33 PM · destroydelaydestroy objectdestroygameobjectdestroy-clones

Destroy Objects after a set time

I know this question has been asked before, but I am still confused and have tried multiple methods (like Object.Destroy(obstacle, 2.0f and such) and all has failed. I am coding a game that drops obstacles (20) onto a plane, and Every 5 seconds I want the game to destroy the objects and make new ones fall (So it doesn't clog the game and new objects can form) My code is:alt text

screenshot-4.png (69.7 kB)
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

2 Replies

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

Answer by RendrWyre · Jan 09, 2020 at 08:34 PM

You could attach a script to the GameObject prefab that you spawn that is responsible for destroying itself after 5 seconds. There's probably a more elegant way to do it within a single script with lists or something but maybe you just want something quick and simple.

 Start()
 {
     StartCoroutine(SelfDestruct());
 }

 IEnumerator SelfDestruct()
 {
     yield return new WaitForSeconds(5f);
     Destroy(gameObject);
 }
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
1

Answer by Owlfrog · Jan 09, 2020 at 08:12 PM

When you're instantiating the obstacle, it returns a reference to the game object that is created. So one approach you might be able to take is adding a list to the class; and then when the time is up, loop through and destroy the objects in the list and clear the list.

Something like this (note this is not compilable code, since I couldn't copy & paste your screenshot code :) )

 class MyObstacleSpawner {
     List<GameObject> objects = new List<GameObject>();
     float timeToRecreateObjects = 5f;

     void Update () {
         if (timeToRecreateObjects <= 0f) {
             SpawnAllObstacles();
             timeToRecreateObjects = 5f;
         } else {
             timeToRecreateObjects -= Time.deltaTime;
         }
     }

     void DeleteObjects() {
         for(int i = 0; i < objects.size; i++) {
             objects[i].Destroy();
         }

         objects.clear();
     }

     void SpawnAllObstacles() {
         for (int i = 0; i < obstacleCount; i++) {
             SpawnObject()
         }
     }

     void SpawnObject() {
         Vector3 pos = Vector3.Zero;
         objects.add(Instantiate({Parameters here}));
     }
 }
Comment
Add comment · Show 2 · 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 IINovaII · Jan 09, 2020 at 08:22 PM 2
Share

Ins$$anonymous$$d of just destroying the objects, in my opinion, it would be much better to add them to an object pool. That way, if you have any objects in the object pool you can use them ins$$anonymous$$d of creating new ones to improve performance.

avatar image RendrWyre IINovaII · Jan 09, 2020 at 08:49 PM 0
Share

True. OP does appear to already know the maximum number of gameobjects (20) and it would remove the need to allocate and deallocate memory on instantiation and destruction.

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

126 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 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

Script doesn't work anymore! 2 Answers

Destroy() not working on prefab instance 2 Answers

How do I Destroy a Child after Instantiating it? 1 Answer

Difference between Destroy and Destroy immediate (In this case). 2 Answers

UI Object is there but can't destroy it. 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