• 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 SYRSA · Dec 18, 2013 at 03:20 AM · ideas

Is there a low performance lifetime limiter?

I'm looking for an efficient way to limit the lifetime of instantiated gameobjects which are atoms that disappears after a set amount of time so they don't overload the game. There are going to be well over a hundred atoms, and I'm looking for a way to make them have a lifetime that won't lag the game when I attach them on each atom.

I tested these 2 scripts to be attached on each atom, but they really wreck the fps when there are just 50 atoms in the scene. Any ideas?

 var Seconds = 10;
  
 function Update(){
     Destroy();
 }
  
 function Destroy(){
     yield WaitForSeconds(Seconds);
     Destroy(gameObject);
 }



     var lifetime = 1.1;
  
     function Awake()
     {
         Destroy(gameObject, lifetime);
     }
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 AlucardJay · Dec 18, 2013 at 03:24 AM 1
Share

In your first example, you should be calling Destroy() from the Start function, not the Update function

avatar image SYRSA · Dec 18, 2013 at 03:43 AM 1
Share

Thank you for pointing out my derpness. And I do appreciate that :P

avatar image iwaldrop · Dec 18, 2013 at 06:41 AM 0
Share

Plus one for the word 'derpness'.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Kiloblargh · Dec 18, 2013 at 03:47 AM

Don't instantiate or destroy them. Create all the atoms you will need at startup ( maybe using some yields in the Instantiate loop so it doesn't lock up the game) and immediately set them inactive and put them in a List.< GameObject >.

Then, set them active again one by one as needed. Doing it this way will produce little or no lag.

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 SYRSA · Dec 18, 2013 at 05:00 AM 0
Share

The thing is that these atoms are actively spawning throughout the gameplay from stars, molecular clouds, etc. I need each atoms to have a lifetime. But alucardj already fixed the problem with the first script, and it is working fine now.

avatar image iwaldrop · Dec 18, 2013 at 06:42 AM 0
Share

I wouldn't be so quick to dismiss the concept of this answer (or tbkn's), as they're more correct than you probably realize.

avatar image AlucardJay · Dec 18, 2013 at 11:30 AM 0
Share

I was just commenting on the code in the question. You should seriously consider pooling your objects.

avatar image
3

Answer by Tomer-Barkan · Dec 18, 2013 at 06:33 AM

Performance wise, you can use a a stack of GameObjects that grows as necessary and archives the atoms after their lifetime expires. Here's a class that can do that for you:

 public class AtomCreator : MonoBehaviour {
     public GameObject atomPrefab;
     
     private Stack<GameObject> atomStack = new Stack<GameObject>();
     
     // enables an atom from the stack, if available. Otherwise creates a new one.
     public GameObject CreateAtom(float lifetime) {
         GameObject atom;
         if (atomStack.Count > 0) {
             atom = atomStack.Pop();
             atom.SetActive(true);
         } else {
             atom = GameObject.Instantiate(atomPrefab) as GameObject;
         }
         
         // destroy atom after lifetime expires
         if (lifetime > 0) {
             StartCoroutine(DestroyAtom(atom, lifetime));
         }
         return atom;
     }
     
     // disables the atom and adds it to the stack
     public IEnumerator DestroyAtom(GameObject atom, float delay) {
         yield return new WaitForSeconds(delay);
         atom.SetActive(false);
         atomStack.Push(atom);
     }
 }

Call CreateAtom() with the lifetime you want it to have. It will either create a new one or reuse one from the stack if avaialable, and will return the created object so you can further manipulate it (like setting it's position after creation). It will also start the coroutine to deactivate it after its lifetime expires, so it will automatically be deactivated and added to the stack, for future use. This is done automatically as long as you provide a lifetime that is greater than 0.

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 Kiloblargh · Dec 18, 2013 at 07:36 PM 0
Share

That's awesome, I was previously unaware that Stack was even a thing. Is it significantly faster than List.Insert (0,item)?

avatar image Tomer-Barkan · Dec 18, 2013 at 08:57 PM 0
Share

Stack Push and Pop operations are O(1), which is the best performance possible. List Add operation (adding to the end of the list) is O(1) as well, and there's no reason to use Insert, since you just want to add it to the pool, the position doesn't matter. Still, removing from the list, is O(n) operation, which is not as efficient as removing from the stack, so in total, using a stack will be more efficient.

But anyway, the idea of reusing your objects ins$$anonymous$$d of destroying and creating them every time is the real optimization here, since those operations are more expensive than the list or stack operations.

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

20 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

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Obstacles won't destroy on collision? 1 Answer

Reloading Help 2 Answers

Scripting Error! - UCE0001: ';' expected. Insert a semicolon at the end. 1 Answer


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