- Home /
Problems with "Overhead"
I am having a problem with my game. The game runs pretty smoothly most of the time. That is, until you pause it. Whenever I paused the game, one of my cloths would disappear and there is incredible lag. I originally blamed it on the cloths, so I disabled them and replaced them with a plane renderer. But the problem persisted, so I opened the profiler and I found this:
I have looked around on Google and Unity Answers and I cannot find anything that has helped me. Everyone else is having the problem when first starting the game. I am only having it when I pause the game. The beginning of the red spike is where I pressed Esc and paused the game.
Here is my pause code:
using UnityEngine;
using System.Collections;
public class PauseMenu : MonoBehaviour {
public bool isPaused = false;
public bool canPause = true;
public Font font;
void OnGUI() {
if(!isPaused) {
return;
}
GUIStyle style = new GUIStyle();
style.font = font;
style.fontSize = 50;
style.normal.textColor = Color.white;
GuiUtils.drawLabelWithShadow(new Rect(10, 10, 200, 200), "Paused", style);
style.fontSize = 25;
if(GUI.Button(new Rect(10, 250, 100, 50), "Resume", style)) {
isPaused = false;
}
}
void Update () {
Time.timeScale = isPaused ? 0f : 1f;
Camera.main.GetComponent<BlurEffect>().enabled = isPaused;
if(Input.GetKeyDown(KeyCode.Escape)) {
isPaused = !isPaused;
}
if(!canPause) {
isPaused = false;
}
foreach(Animation animation in (Animation[])GameObject.FindObjectsOfType(typeof(Animation))) {
animation.enabled = !isPaused;
}
}
public static PauseMenu getPauseMenuInstance() {
return (PauseMenu)GameObject.FindObjectOfType(typeof(PauseMenu));
}
}
Can anyone help me? I've been having this problem for a while, so any help would be greatly appreciated! I have made pause menus in the past that work the EXACT same way as this one, but I've never encountered this problem.
Thank you! Romejanic
Answer by copperfeld · Feb 11, 2015 at 09:46 AM
hope this helps: first i noticed the FindObjectsOfType() method. Better not use expensive finder calls in an Update method, which is called once per frame. But that is probably not the (only) reason for your drag in pause mode.
'GUIStyle style = new GUIStyle();' -> also, I'd rather avoid to create new objects on each call of 'OnGUI', because it's again expensive and makes the garbage collector running, which will slow down your game most likely.
I recommend you to do both things ahead and see if it helps; move them into Start() or Awake() methods, for instance.
Thank you so much! I moved the GUIStyle into a variable but I still had problems, so I moved all of the stuff that stayed static (font, text color, etc) into Start() and it works perfectly! I think it even took load off of the game anyway! You are the best, thank you so much!
On my 27" i$$anonymous$$ac, the title screen runs at 2000fps ^-^