Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Mauhan · May 21, 2017 at 03:43 PM · buildscript.looplagfreeze

Scripts freezes game for a few seconds after build.

Hello guys. Im working on Inventory. I have problem with game after build only. I have script for add items to slot.

 public static void setInSlot(Item item)
 {
     for(int i = 0; i < playerItems.Count; i++)
     {
     if(playerItems[i].ID == item.itemID)
         {
             if(playerItems[i].amount < item.maxStack)
             {
                 playerItems[i].amount++;
                 return;
             }
         }
     }
     for(int i = 0; i < Slots.Count; i++)
     {
         if(Slots[i].transform.childCount == 0)
         {
             GameObject itemObj = Instantiate(itemprf);
             itemObj.name = item.itemID.ToString();
             playerItems.Add(itemObj.GetComponent<itemObject>());
             itemObj.GetComponent<itemObject>().ID = item.itemID;
             itemObj.transform.SetParent(Slots[i].transform);
             itemObj.GetComponent<Image>().sprite = item.itemSprite;
             itemObj.transform.localPosition = Vector2.zero;
             itemObj.transform.localScale = Vector3.one;
             break;
         }
     }
 
 }

There are 2 loops. 1st checks max stack of object. Second checks free slots to add items there. In editor everything works good. There are no freezes, no lags etc. Only after build I have problem with it. When I want to add item game get lag for 3/5 seconds. But freeze happens only one, first time. It looks that the script is loading for first time or something. So is any way to make this better? Or to fix that lags? Thank you in advance:)

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
1
Best Answer

Answer by FM-Productions · May 21, 2017 at 04:08 PM

Hi,

I have encountered something similar. In my case, it is almost certain to say that the first time a complex prefab is Instantiated will cause my game to lag. In your line 18, Instead of instantiating, you could make a pool of itemprf objects and maybe set them inactive. If you need a itemprf gameObject you simply take one from your pool and set the gameObject active. Another tip in terms of performance is to avoid functions like GetComponent in Update() functions (or functions that are executed often).

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 Mauhan · May 21, 2017 at 04:37 PM 0
Share

You're right. I'll try it. Thank you!:)

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

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Nested for-loop freezing Unity 0 Answers

Web Player occasionally lags browser 1 Answer

Help! Unsaved work stuck in frozen unity, believed to be from infinite loop. What can i do? 2 Answers

Unity 5 64x Build bug ? 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