- Home /
2.5ms update
I have a update function which takes up 2.5ms (10-20%), the function looks like this, and should require that much to run. Why does it require that much to do such simple update function?
function Update() {
if (Network.isServer || Network.isClient) {
var cam = GameObject.Find("Menu Camera");
if (cam) {
Destroy(cam);
}
if (Input.GetKeyDown(KeyCode.F1)) {
ShowInfo = !ShowInfo;
Game.SetMouse(ShowInfo);
}
if (Input.GetKeyDown(KeyCode.Escape)) {
ShowPauseMenu = !ShowPauseMenu;
ShowPMenu = ShowPauseMenu;
UpdateMouse = true;
Game.SetMouse(ShowPauseMenu);
if (!ShowPauseMenu) {
ShowGraphicsMenu = false;
ShowAudioMenu = false;
ShowControlsMenu = false;
ShowOptionsMenu = false;
ShowGameplay = false;
ShowCredits = false;
}
}
if (Game.player == null && !allowQuit) {
//This function is only called 1 time each respawn.
if (!ShowLoading) {
ShowRespawnScreen = true;
}
if (Game.gameobject.GetComponent(ProceduralTerrain).GeneratedTiles == 1) {
Game.player = SP(SpawnPos,Respawn);
AstarPath.active.astarData.gridGraph.center = Vector3(Game.player.transform.position.x,Game.gameobject.GetComponent(ProceduralTerrain).terrainHeight,Game.player.transform.position.z);
AstarPath.active.astarData.gridGraph.GenerateMatrix();
AstarPath.active.Scan();
ShowLoading = false;
}
}
}
}
Answer by DaDonik · Jan 26, 2015 at 04:42 PM
You do that every Update O.o
GameObject.Find("Menu Camera");
This is your problem right here...
So, moving that line to my start function would fix it?
Yeah, caching the camera is the way to go, just like meat5000 says.
While you're at it, don't do this every frame either:
Game.gameobject.GetComponent(ProceduralTerrain)
And what are the following functions doing? Are you recalculating an A* path every frame?
AstarPath.active.astarData.gridGraph.Generate$$anonymous$$atrix();
AstarPath.active.Scan();
AstarPath.active.astarData.gridGraph.Generate$$anonymous$$atrix();
AstarPath.active.Scan();
As the comment says, they are only called once when the player respawns (The scene doesn't reload) Since I would have to recreate the Terrain again then.
Answer by meat5000 · Jan 26, 2015 at 04:42 PM
var cam = GameObject.Find("Menu Camera");
Dont do this in Update. Find it once before update begins and store it.
I see you find it to destroy it. Once its destroyed you still keep looking for it. This will take longer than actually finding it! Make a one-shot bool to switch this off once it has been performed.
I'd do it this way assuming that this step needs to be performed where you've placed it. Start may not cut it.
var myFlag : boolean = false;
function Update()
{
if(!myFlag)
{
Find Cam
Destroy Cam
myFlag = true
}
}
You get the idea.