- Home /
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?
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
It only hiccups when I setActive the object for the first time second times works perfect.
What kind of code do you have in the Start function of the objects which you are activating?
only have a float time increasing with deltaTime until max time and setActive(false) the object.
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.
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
![](https://koobas.hobune.stream/wayback/20220613161956im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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