Our game is running really smoothly except on a few frames that take 300% of the usual time, and even worse on the iPhone(our target), causing a visible lag.
The strange thing is that the profiler says that the lag comes from completely trivial methods that usually execute in an instant. (like taking several ms for removing an element from a very small list…) It looks like the lags come the first time those key methods are invoked.
This strange behaviour is consistant and the results and graph shown below can be easily be reproduced.
Here is an example of the code that supposedly takes 50% of a very slow frame …
public class MediumEnemy : Enemy {
override public void Die(){ // 50% of frame time, self 5.8%
game.SpawnBonusUpgrade(posForward,posLateral); // 12%
base.Die(); // 30%
}
}
public class Enemy : PathEntity{
override public void RemoveFromScene(){ // 27%, self 5.8%
game.GetEnemies().Remove((Enemy)this); // 19.2%
base.RemoveFromScene();
}
}
public class PathEntity : MonoBehaviour{
Game game;
float posForward,posLateral;
GameObject pathFollower; // an empty game object
virtual public void Die(){ // 30% of frame time, self 2.8%
if(diePrefab != null){
GameObject explosion = (GameObject)Instantiate(diePrefab,transform.position, transform.rotation); //1.2%
if(diePrefabDuration > 0.0f){
Destroy(explosion,diePrefabDuration); //0.1%
}
}
Destroy(this.gameObject,0.0f); // 0.1%
RemoveFromScene(); //Calls Enemy.RemoveFromScene() // 27.0%
}
virtual public void RemoveFromScene(){ //1.9%
Destroy(pathFollower.gameObject);
Destroy(this.gameObject);
}
}
public class Game : MonoBehaviour {
List<Enemy> gameEnemies; //never more than 10 elements
public List<Enemy> GetEnemies(){
return gameEnemies;
}
}
Afterwards, the same code executes nearly instantly (as expected)
Here is a capture of Unity showing the data above
http://www.mentalwarp.com/~g0/lag_01.png
Any ideas ?