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?

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.