- Home /
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!
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;
}
its a 2d game and seems like FindObjectsOfType trying to find objects every frame
so don't use FindObjectsOfType in LateUpdate.
If truly, I don't remember to use FindObjectsOfType anywhere on my scripts..!
Oh, I found this type on assest's scripts. So I need to replace this somehow?
Are you sure those are related? IN the profiler it says DynamicLight.LateUpdate
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
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.