Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by GesterX · Apr 10, 2011 at 02:55 PM · prefabperformancelag

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GesterX · Apr 11, 2011 at 08:23 AM 0
Share

Ah okay. I'll try doing that then!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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

Big Jerk or Lag when starting the game in Iphone caused by Shader.CreateGPUProgram, BatchRenderer.Flush and MeshSkinning.Render 0 Answers

Instantiating in Awake() v Setting up prefab instances in Editor performance difference? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges