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 GameDevSA · Apr 03, 2017 at 06:51 AM · optimizationalphasetactiveactive

Is it cheaper to deactivate a game object or change its alpha, or other?

Hi guys,

I am making a space shooter and want a simple optimization question. I have a pool of flame particles that trail behind the ships, and when a ship is destroyed, I'm wondering if it would be cheaper for me to se their alphas to 0 and let their animations continue, or just deactivate the game objects?

The ships themselves are also part of the field and get re-used, so they will keep the flame particles with them.

The question really is, is it cheaper to turn the alpha on and off of a bunch of objects, or to deactivate and reactivate a bunch of objects?

Or is there a third and better solution, ie, turning off the sprite renderer or some other option?

Thanks for your time

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 SohailBukhari · Apr 03, 2017 at 07:14 AM 0
Share

It would be better to deactivate the gameobjects because at start they will assign memory and according to your need you can enable and disable. If you destroy your gameobject then when you re-instantiate gameobject it will assign memory block again and during game running instantiating is bad way and sometimes it makes your gameplay leggy. so i think deactivating will be good technique.

avatar image hexagonius SohailBukhari · Apr 03, 2017 at 07:23 AM 0
Share

i think the question was disable renderer vs SetActive, not SetActive vs Destroy/ instantiate

avatar image hexagonius · Apr 03, 2017 at 07:40 AM 0
Share

usually toggling specific components is cheaper than changing the state of the whole gameobject. the question is if you're die not to forget anything, especially scripts which can make use of OnEnable/ OnDisable.
I consider SetActive safer, but in cases of particles that should have their time to fade out, out either needs a delay or a separate particles object. otherwise they just get cut off.
but overall, it's easier to use the profiler to find out of it's even an issue.

avatar image GameDevSA hexagonius · Apr 03, 2017 at 08:21 AM 0
Share

ah ok. In this specific scenario I have a pool of particles. It was screweing up their animation when I deactivated them and tried to restart it all again, and was wondering whether it was a good idea just to turn the sprite renderer off and leave them being moved via their script, so that when I turn the renderer back on they will still be there.

But, that would mean I would be leaving it on for all of the space ships, as they will all be created as a pool at the start of the level and intended as a webGL game.

1 Reply

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

Answer by tanoshimi · Apr 03, 2017 at 07:34 AM

Destroying an object releases its memory, means it no longer uses any CPU time (since none of the Update() functions on its attached components will be called), but causes the garbage collector to have to reclaim the memory and, if you want to use it again, means it will have to be reinstantiated. Only use this for objects that you're certain you don't want to instantiate again.

Deactivating a gameobject keeps its memory allocated, but no longer uses any CPU time (Update() etc. on deactivated objects is not called), and creates no additional work for the garbage collector. Reactivating it is a trivial operation.

Disabling the sprite renderer keeps the object's memory allocated, and continues to use CPU time since Update() etc. will still get called, but doesn't cause any work for the renderer since nothing gets drawn to screen.

Setting alpha to zero continues to use all the memory, CPU, and rendering resources as normal - it's just you can't see the object. Transparent objects still generate just as much work for the rendering pipeline - it's just that at the point a pixel is drawn to the screen (normally at the very last step of the fragment shader), the blend mode means it has no influence on the final colour. I can't think of any situation where you'd want to do this.

Unless you have a very specific reason to keep your memory footprint to an absolute minimum, I'd recommend deactivating the object for the vast majority of cases.

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 GameDevSA · Apr 03, 2017 at 07:38 AM 0
Share

Thanks so much for that insightful response! It's very helpful. Deactivating objects was causing other weird issues for me so I was hoping to find a way around it, but guess I'll just have to try and solve that. Thanks very much!

avatar image GameDevSA · Apr 03, 2017 at 07:39 AM 0
Share

Does deactivating an object also deactivate it's components?

avatar image tanoshimi GameDevSA · Apr 03, 2017 at 07:55 AM 0
Share

Update()/OnTriggerEnter() etc do not get called on an inactive object, and you can't grab a reference to an inactive object using Gameobject.Find(). But you can still explicitly call methods in components on a deactivated object if you already have a reference to them.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

What- if anything- is wrong with this shader (alpha/reflective/unlit)? 1 Answer

Optimize code changing transparency of large number of Objects 0 Answers

About Optimization, Garbage Collection, Unused Scripts. 1 Answer

C# iOS - .active, SetActive(false) 0 Answers

SetActive() not working 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