- Home /
Framerate drop due to method call
Hello,
Whenever I call UpdateChunks my framerate drops greatly, from ~120 fps to anywhere below 40 fps.
What in the method could be causing this? Additionally: The method is being called from Update.
void UpdateChunks ()
{
int myX = Mathf.FloorToInt (transform.position.x / chunkSize) * chunkSize+5;
int myZ = Mathf.FloorToInt (transform.position.z / chunkSize) * chunkSize+5;
int range = viewSize / 2;
int count = 0;
for (int i = myX-range; i < myX+range; i += chunkSize) {
for (int j = myZ - range; j < myZ + range; j += chunkSize) {
Vector3 checkPos = new Vector3 (i, 0, j);
checkPositions [count] = checkPos;
if (checkPositions [count] != chunkPositions [count]) {
Destroy (chunks [count]);
chunks [count] = (GameObject)Instantiate (chunk, checkPos, Quaternion.identity);
}
count++;
}
}
}
Answer by MaxGuernseyIII · Oct 25, 2017 at 07:28 PM
Process of elimination would suggest these lines:
Destroy (chunks [count]);
chunks [count] = (GameObject)Instantiate (chunk, checkPos, Quaternion.identity);
Can you comment out the lines and see if you still get the framer ate drop?
When I comment out the Instantiation call it does fix the framerate drop. Is it possible that the chunks are constantly being destroyed than re-instantiated to the point that it causes the framerate to drop?
I think this may be the case. I cannot select the instanced objects in the hierarchy during runtime. It is as if they are being destroyed then re-instantiated over and over. What in my method is causing this? I know it must have to do with the way my loops run.