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
0
Question by Legend of Hibiki · Aug 31, 2014 at 04:11 PM · instantiatepoolinghiccup

PoolObject hiccup

Hello guys,

I'm having a problem that is freaking me out.

So a Have a code like that

 List<GameObject> list = new List<GameObject>();

 Start(){    
    
 
    for(in i = 0; i< 10; i++){
       Transform clone = (Transform) Instantiate (prefab, vector, quart);
       list.add(clone.gameObject);
    }
 }
 public GameObject getPrefab(){
       foreach(GameObject g in list){
            if(!g.activeInHierarchy){
                 g.position = ....
                 g.rotation = ....
                 g.setActive(true);
                 return g;
            }
       }
 }


So i have that functions but the first time that I get a prefab game have a hiccup, but the secound don't happens the hiccup. I can't understand why because all objects are instantiated.

Example 3 lists ListA - 5 obj , ListB - 2 obj , ListC - 3 obj

 listA.getPrefab() hiccup
 listA.getPrefab() no hiccup
 ListB.getPrefab() hiccup
 ListC.getPRefab() hiccup
 ListB.getPrefab() no hiccup
 ....

Hiccup only happens the first time that I set one gameobject from a list to visible, but if listA and ListB are list of same gamebject it only hiccup in the first list that calls getPrefab().

Any type of solution?

Comment
Add comment · Show 4
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 BMayne · Aug 31, 2014 at 05:24 PM 0
Share

Try using a

for(int i = 0; i < list.Length; i++)

Foreach loops don't really like you editing their values sometimes. Foreach loops are slower then for loops anyway :)

No promises that this will work.

Regards

avatar image Legend of Hibiki · Aug 31, 2014 at 06:27 PM 0
Share

It only hiccups when I setActive the object for the first time second times works perfect.

avatar image Bunnybomb7670 · Aug 31, 2014 at 09:39 PM 0
Share

What kind of code do you have in the Start function of the objects which you are activating?

avatar image Legend of Hibiki · Aug 31, 2014 at 09:48 PM 0
Share

only have a float time increasing with deltaTime until max time and setActive(false) the object.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Kiwasi · Sep 01, 2014 at 12:53 AM

Start runs before the first frame of Update. So the hiccup you see is the cost of instantiating all of the list items. If an object is created in your scene, but not enabled, then Start will not run until you enable it.

Possible fixes

  • Move code to Awake. This will do all your instantiating during level load, when the user is expecting things to go slow.

  • Create an empty list and instantiate prefabs one at a time as you need them. This will spread the cost of the hiccup out over several frames.

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 Kiwasi · Sep 01, 2014 at 12:56 AM 0
Share

I'm pretty sure Awake will still run on a disabled GameObject. If it doesn't an alternative solve is to start with the GameObject enabled, then disable it at the end of Awake, after the instantiation is done.

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

25 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

Related Questions

IOS Object Pooling 0 Answers

Get components of multiple instantiated objects? 2 Answers

how to create random list of two diffrent prefab 1 Answer

Pooling object performs the same or worse than instantiate/destroy? 1 Answer

Problem with object pooling and reseting the properties of a pooled object 0 Answers


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