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 /
This question was closed Jan 06, 2017 at 01:20 AM by JoshBaz for the following reason:

The question didn't really have a definite answer but people in the comments helped to give me the advice/information I needed.

avatar image
0
Question by JoshBaz · Jan 05, 2017 at 08:14 PM · gameobjectgameobjectsenabledenable and disable scriptdisabled

What is the best way to manage gameobjects in a scene to conserve memory?

Hi. I'm writing a script to manage active gameobjects in a scene. What the script is supposed to do is disable/enable the object based on how close the player gets to the objects position. I have a 7 objects being managed by this script and these seven objects are distanced away from each other and has a script attached to it that makes it orbit around Vector3.zero.

What I want to know is it better memory wise to just leave all the gameobjects enabled in the scene or to disable the gameobject and then enable it when the player gets close. The point is to conserve memory. I could just delete the gameobject and load it from Resources when the player gets close but I don't want all the lag that comes from Resources.Load or Resources.LoadAsync so I'm trying to avoid that. What should I do?

Code example in case anyone needs reference:

 public Transform player;
 public GameObject[] objects;
 public float maxDistance = 60f;
 
 void Update () {
 for(int i = 0; i < objects.Length; i++){
 float distance = Vector3.Distance(objects[i].transform.position, player.position);
 
 if(distance > maxDistance){
 if(objects[i].activeSelf)
 objects[i].SetActive(false);
 }else{
 if(!objects[i].activeSelf)
 objects[i].SetActive(true);
 }
 }
 }

Comment
Add comment · Show 9
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 hoekkii · Jan 05, 2017 at 08:34 PM 1
Share

Indeed destroying and creating will not be a good practice. $$anonymous$$emory wise it will take more to manage the enable/disable state (you have some extra references and stuff) than just leaving them on, but it is just negligible. You will gain performance, but first make sure if you have to optimize that part of the game, because I doubt it is that much worth optimizing it. If you do have performance issues, look where the problem is (use deep profiling for example). Or lower the quality of the shaders/shadows/models or use LOD. If you have to optimize the part that you describe yeah turning them off when they are far away disable them, make sure you do not use magnitude but sqrmagnitude because it Sqrt is quite heavy and unnecessary. SetActive(transform.localPosition.sqr$$anonymous$$agnitude < (_sqrDistance)); // where _sqrDistance is cached -> _sqrDistance = _distance * _distance

avatar image tanoshimi · Jan 05, 2017 at 08:54 PM 0
Share

And are these 7 objects doing something very processor-intensive when they're enabled? Because if not, like @hoekki says, this sounds like a case of premature optimisation.

avatar image JoshBaz tanoshimi · Jan 05, 2017 at 09:32 PM 0
Share

The seven objects are planets I have orbiting a star and rotating on their axis. The star is just a Lens Flare and the planet models are extremely simple.

avatar image tanoshimi JoshBaz · Jan 05, 2017 at 10:58 PM 0
Share

In that case I highly recommend you do nothing at all and let Unity do its automatic management tasks (such as culling renderers that aren't in view, putting rigidbodies to sleep etc.). With so few objects, the overhead of your manual management system is almost sure to exceed any possible benefits it would offer.

avatar image Clashstrats · Jan 06, 2017 at 12:14 AM 0
Share

Could you share a screen shot of the code? I cant help unless i know how this is being accomplished. As long as its c#

avatar image JoshBaz Clashstrats · Jan 06, 2017 at 01:08 AM 0
Share

I added the code example

avatar image Clashstrats JoshBaz · Jan 06, 2017 at 01:15 AM 0
Share

I have to agree with the above ppl. Your way below the line for time to start optimizing. And honestly, unity does a good job of doing it in tbe background anyway. Just a thought, id change the void update to FixedUpdate, it'll run smoother if you do ever get to the point where this code becomes useful in the game. Hope that helps.

avatar image JoshBaz · Jan 06, 2017 at 01:18 AM 0
Share

Thanks everyone for the advice. It really helped to better my understanding of what to do.

avatar image Hanoble · Jan 06, 2017 at 02:25 AM 0
Share

$$anonymous$$emory wise once the object is there in the scene, it is in memory. Enabling or disabling that object does not add and remove it from actual memory, thus the entire concept behind your script is pointless. On top of that, Unity GameObjects are very small (we are talking bytes here) and an optimization at that level is almost certainly not going to make an impact on your game.

There are a handful of VERY specific optimization techniques in which people are managing memory in such a way to never run garbage collection (there was a Unite talk from the Playdead guys who used a technique like this for their title Inside), but optimizations at this level for memory purposes alone are typically futile.

You can confirm all of that by using the profiler, which is something I would suggest you spend some time getting comfortable with so that when you do need to optimize, you can better find where to make those optimizations.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

80 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

Related Questions

Enable & Disable multiple game objects 0 Answers

If i disable a GameObject does all it's components get disabled also? 2 Answers

How do I enable my scripts on default?? 3 Answers

How do you change size of an object by its units rather than scale? 1 Answer

If Check Colliding GameObject Variable 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