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
0
Question by Rembo4Fight · Feb 15, 2018 at 01:15 PM · scripting problemfpsprofilerfindobjectoftype

Profiler FindObjectsOfType very slowing down game.

It try to detect destroyed objects, like my collectibles

And some times is freezes and sometime it slowing down fps very much

How to decrease this effect or maybe unable it?

It very strange that is slowing down everything because its a 2d game and seems like FindObjectsOfType trying to find objects every frame

Please try to help!alt text

Maybe it's something on the script, but its not..

 void OnTriggerEnter2D(Collider2D col) 
     {
         if (col.gameObject.layer == LayerMask.NameToLayer ("food")) {
             Destroy (col.gameObject);
             score++;
         }


here is the problem code

 // ----------- MAIN UPDATE ----------- //
                 private void LateUpdate()
                 {
                     if (_meshFilter.sharedMesh == null) {
                         Rebuild ();
                         return;
                     }
                             
 
             // ------------- TYPE OF LIGHT ---------------------- //
                     if (light2DType != Light2DType.Dynamic && lastLight2DType != Light2DType.Dynamic)
                         staticScene = true;
                      else
                         staticScene = false;
                     
 
                     if (light2DType == Light2DType.OnlySight && lastLight2DType != Light2DType.OnlySight) {
                         doNotRenderMesh = true;
                         useEvents = true;
                         Rebuild ();
                     }else if(light2DType != Light2DType.OnlySight)
                         doNotRenderMesh = false;
                         
                     if (light2DType == Light2DType.OneFrame && Application.isPlaying)
                         StartCoroutine (deactivateItself (.2f));
                         
 
                     lastLight2DType = light2DType;
             //-----------------------------------------------------//
                 
                 #if UNITY_EDITOR
                         //Adding listeners
                         if(permissionToAddListeners == true)
                         {
                             permissionToAddListeners = false;
                             AddListeners();
                         }
 
                 #endif
                         fixedLimitations();
 
                         //Sorting
                         if(_meshRenderer.sortingOrder != SortOrderID) _meshRenderer.sortingOrder = SortOrderID;
 
 
                         // Reaload _allMeshes when is in editor mode //
                         if(!Application.isPlaying){
                                 GetCollidersOnScene();
                                 _forceToRefresh = true;
 
                         }else{
                                 if(staticScene == true){
                                         // Only reload when is notificated 
                                         if(reloadMeshes == true){
                                                 GetCollidersOnScene();
                                                 reloadMeshes = false;
                                         }
                                 }else{
                                         //GetCollidersOnScene();
                                         GetCollidersOnSceneNonStatic();
                                 }
                         }
                         //--------------------------------------------------
                         ColliderState anyColliderChange = DidAnyColliderTMChange();
 
 
 
 
                         if (anyColliderChange == ColliderState.HasBeenDeleted) {
                                 reloadMeshes = true;
                                 _forceToRefresh = true;
                                 return;
                         }
 
 
                         if ((anyColliderChange != ColliderState.NoChanged) || (transform.position != _lastPos) || (transform.rotation != _lastRot) || (_forceToRefresh) || (LightRadius != _lastRadius))
                         {
 
                                 // Restore force to reload all meshes //
                                 _forceToRefresh = false;
                                 _lastPos = transform.position;
                                 _lastRot = transform.rotation;
                                 _lastRadius = LightRadius;
 
 
                                 // First check if any collider has been removed //
                                 if (anyColliderChange == ColliderState.HasBeenDeleted)
                                 {    reloadMeshes = true;
                                         _forceToRefresh = true;
                                         
                                 }
 
 
                                 // since this is rather expensive let's chop it up
                                 _allVerts.Clear();// Since these lists are populated every frame, clear them first to prevent overpopulation
 
                                 // Reset List GameObjects touched by light
                                 resetReachedEvent();
 
 
                                 // Obtain vertices for each mesh
                                 GenerateColliderVerts();
 
                                 // Generate vectors for light mesh
                                 GenerateLightVectors();
                             
                                 // Reorder the verts based on vector angle
                                 SwapVertOrders(ref _allVerts);
 
                                 // Render the final mesh wit allvert collected            
                                 RenderLightMesh();
 
                                 // reset mesh bounds
                                 ResetBounds();
                         }            
 
                 }
 
 
                 private IEnumerator deactivateItself(float t){
                     yield return new WaitForSeconds (t);
                     GetComponent<DynamicLight> ().enabled = false;
                 }
 
 
 
                 private enum ColliderState{
                         NoChanged,
                         HasChanged,
                         HasBeenDeleted
                 }
 
                 /// <summary>
                 /// Checks if any of the colliders transformation has changed
                 /// </summary>
                 /// <returns>0=false</returns>
                 /// <returns>1=true</returns>
                 /// <returns>-1=killed</returns>
                 private ColliderState DidAnyColliderTMChange()
                 {
 
                         for (int i = 0; i < _colliders.Length; i++){
 
                                 if(_colliders[i].collider.transform == null)
                                         return ColliderState.HasBeenDeleted;
 
                                 if(_colliders[i].DidTransformDeleted)
                                         return ColliderState.HasBeenDeleted; // has been deleted
 
 
                                 if (_colliders[i].DidTransformChange)
                                         return ColliderState.HasChanged; // has been changed
 
                         }
 
                         return ColliderState.NoChanged;
                 }



screen-shot-2018-02-15-at-161207.png (311.2 kB)
Comment
Add comment · Show 15
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 Prastiwar · Feb 16, 2018 at 07:50 AM 0
Share

its a 2d game and seems like FindObjectsOfType trying to find objects every frame

so don't use FindObjectsOfType in LateUpdate.

avatar image Rembo4Fight Prastiwar · Feb 16, 2018 at 07:51 AM 0
Share

If truly, I don't remember to use FindObjectsOfType anywhere on my scripts..!

avatar image Rembo4Fight Rembo4Fight · Feb 16, 2018 at 07:53 AM 0
Share

Oh, I found this type on assest's scripts. So I need to replace this somehow?

Show more comments
Show more comments
avatar image Pangamini · Feb 16, 2018 at 03:04 PM 0
Share

Are you sure those are related? IN the profiler it says DynamicLight.LateUpdate

avatar image Rembo4Fight Pangamini · Feb 16, 2018 at 03:25 PM 0
Share

Yes, i'm sorry that script is confusing It's from 2DDL asset And yes, that lines of code that I send you are connected with LateUpdate() I'm sorry if script will be huge, but i will try to send just LateUpdate Or I can do easily to just remove this from script?

I will add script to the title

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by dmytro-hreshchik · Feb 16, 2018 at 06:34 PM

FindObjectsOfType is a very costly operation. You should inspect your code closely and remove the calls to FindObjectsOfType in any kind of Update/LateUpdate/FixedUpdate or the OnTrigger events, instead you can register objects in a list or other collection when they are instantiated.

This page should explain why you shouldn't use FindObjectsOfType method. https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity7.html

I'm sorry but I couldn't find any calls to FindObjectsOfType in the posted code. I belive the posted code is incomplete.

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

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

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

Why is FindObjectOfType() called during Camera.Render? It's not in my code. 0 Answers

How do I transition between different animations? 2 Answers

Flashlight activation issue 1 Answer

My player's camera displays jerky objects 2 Answers

Profiler.CollectGlobalStats high fps problem 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