Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Inan-Evin · Aug 13, 2015 at 10:14 PM · destroyoptimizationactivegc

About Optimization, Garbage Collection, Unused Scripts.

So hello everyone, I need to know something about the Unity's process of optimizing unused scripts. So let me explain, I have one huge scene, it's like there are 3-4 floors, some passages and some extra rooms etc. I've created checkpoints, where the player will not be able to return back. For example, there is a huge livingroom, when player is done with it, it goes to the floor below and won't be able to turn back to the livingroom. Now my question is, I already achieve graphics optimization via Occlusion Culling and LOD. But, when the player gets to the lower floors, there will be a lot of inactive objects with gameplay scripts on them. Will this affect the gameplay or the memory a lot? Should I do something like a loading screen, when the player gets down to the lower floors, just for a few seconds, and while at it, run a coroutine to delete all the gameobjects on the upper floor which has scripts on it ( some are disabled, some are enabled ) and then request a Garbage Collection right afterwards? Or should I just leave the objects like that, will the automatic GC process handle those objects?

I don't necessarily delete gameobjects on the gameplay, in every gameplay script I wrote, I did:

gameObject.SetActive(false);

instead of

Destroy(gameObject);

So, considering that there will be a lot of checkpoints, there will be a lot of ( from 10 to - 110 according to the player's process on the level ) unused, both active & inactive gameobjects that have both active & inactive scripts on them, what should I do to prevent those unused objects to affect further gameplay?

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

1 Reply

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

Answer by jmgek · Aug 13, 2015 at 10:56 PM

I would not 'delete/destroy' game objects seeing how that is expensive, set active is fine. There are a few more things you can do.

batching: http://docs.unity3d.com/Manual/DrawCallBatching.html.

Object pooling: https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

You can even go as far as change or turn of materials how far away the user is from the object. Turn off as many Colliders as you can or have the ones you need turn on when the player gets to a position in the game.

If you really need to, yes put checkpoints in the game. I would also suggest to have a manager for the objects you need and not have 100 components that have diffrent memory in them, example: 100 checkpoints with each one calling it's own functions. You should have a manager that checks to see what objects collider has gone off and then call the managing function. If one checkpoint has gone off turn that one off and activate only the next checkpoint in the array. The Collider checks are expensive as well.

Comment
Add comment · Show 9 · 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 jmgek · Aug 13, 2015 at 11:07 PM 0
Share

It won't cause an overload but it's still checking if there is an on trigger every frame, so yes this is fairly expensive dependent on how many objects you have.

It sounds like your interactables checks a collider, right? If you have a lot of objects just write something that will either control each objects state like a object manager, or turn them off when a player is in a room or something similar. It sounds like you should use a manager so the engine is not continuously checking states of objects.

Can you explain a little more about how and what you are doing?

avatar image Inan-Evin · Aug 13, 2015 at 11:14 PM 0
Share

Ofcourse, I have a lot of interactable objects, like doors, drawers, cupboards, any objects that can be found in the household. Now, all of these have Interactable.cs script on them, which checks if the player is in the correct position by checking OnTriggerEnter, if the trigger is correct, then it continues on checking the player angle, camera angle etc. Then if everything is okay, the script enables the UI ( simple interaction UI, press E to open etc. ), then if the player presses the desired key, Interactable.cs sets a gameobject active. That gameobject may have different scripts on them, as I call them Action scripts. So all these actions scripts are dependant on the OnEnable function, since the gameobject that they are attached to will be set active by the Interactable script, and they perform different actions according to the interaction, like calling an animation, or a sound, enabling another interactions etc. So it's basically like this, a lot of Interaction.cs script attached game objects, which has a box collider set as a trigger on them. But I wonder, I know that OnTriggerEnter function still checks if the script is inactive, but will it still check if the game object is inactive?

avatar image jmgek · Aug 13, 2015 at 11:19 PM 0
Share

Oh boy, a lot of overhead for things. One last question until I am able to offer help. Is there a reason you are not using Raycasts? If you are checking the player rotation and position to see if the player is looking at a door that is extremely expensive.

avatar image Inan-Evin · Aug 13, 2015 at 11:24 PM 0
Share

Simply because Triggers have more functionality when it comes to the matters of "area". $$anonymous$$y checks are not done on the "player" side, but on the interactable object's side. The reason for that, for example, I want an object to be interacted only from a certain area around it, not from any angles. If the player is in that area, and if the player is looking at the object, I want it to be interacted. If I were to use raycasts, it would consume lots of performance to point the raycasts to only one area, because I want my checks to be precise.

avatar image Inan-Evin · Aug 13, 2015 at 11:25 PM 0
Share

By the way, other angle checks are done "after" the trigger check. So if the player is not in the trigger, there won't be angle checks.

Show more comments

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

25 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

Related Questions

Instantiate problem 1 Answer

Questions About Allocations - Arrays - Structs and so on 1 Answer

How do you use a pool of objects rather than creating and destroying repeatedly? 8 Answers

What is the difference between these three ways of making an object disappear? 3 Answers

Destroy and instantiate far objects 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