- Home /
Why does my game lag the first time this prefab is loaded?
I have an odd problem with my top down shooter project.
In my game, when the player kills an enemy a 3DText prefab is instantiated that displays how much XP was gained. In the game there are several different types of enemies which give different amounts of XP.
The problem I am getting is that the game noticably pauses (for about 1 second) the first time each type of enemy is killed.
So for example the player kills the first enemy which is a Robot. The first robot that is destroyed causes the game to pause/lag as the XP 3DText prefab is instantiated. When the remaining robots are killed the game does not pause/lag as they die. Then the player moves on to kill some tanks. The first tank that is destroyed makes the game puase/lag when it's XP 3DText is instantiated - the remaining tanks do not cause the game to lag when they instantiate there XP 3DText.
I can imagine this may be hard to follow. The demo can be played here: CLICK and the behaviour is present.
My code for instantiating the prefab is (network stuff is intentional):
@RPC function DeathSequence() { var explode : Transform; Network.Instantiate(explosion, myTransform.position,myTransform.rotation,0);
var multi =
GameObject.FindWithTag("Player").GetComponent(Combo).multiplier;
var myExpText = baseExp*multi;
var aRotation = Quaternion.Euler(0 , 0 , 0);
var instance = Instantiate(xpText, transform.position, aRotation);
print(myExpText);
instance.GetComponent(XPText).ChangeText(myExpText);
instance.GetComponent(XPText).DisplayXP();
Network.Destroy(gameObject);
}
And the XPText functions that get called are:
function DisplayXP() {
yield WaitForSeconds(0.5);
print(textContent);
GetComponent(TextMesh).text = textContent;
iTween.FadeTo(gameObject, 1, 2);
iTween.ScaleTo(gameObject,{"scale":ScaleTo,"time":1,"loopType":iTween.LoopType.pingPong});
yield WaitForSeconds(2);
iTween.FadeTo(gameObject, 0, 0.5);
yield WaitForSeconds(1);
Destroy(gameObject);
}
function ChangeText(valueA) { textContent = valueA.ToString() + "XP"; }
What is causing the game to lag in the manner that it does? Is it just the fact that I'm giving the game quite a lot to do? And if so, why would the lag only happen the first time each type of enemy is killed?
If this is unclear then please let me know!
Answer by Peter G · Apr 10, 2011 at 05:14 PM
A lot of Unity objects are lazy-loaded so their information will be loaded into memory the first time you use them and that may cause your game to lag. Textures are one of the biggest offender, but their are a few others.
The solution is to force Unity to load your objects at the beginning of the scene by instantiating all your prefabs, or assigning a material with the texture someone in the scene so it will be rendered once.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121856im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Are the prefabs stored in RAM or in the Hard disk? 0 Answers
Unity performance issues. 0 Answers
PlayerController Script Causing Terrible Performance (Profiler says FixedUpdate: LogStringToConsole) 4 Answers
Instantiating in Awake() v Setting up prefab instances in Editor performance difference? 1 Answer