- Home /
gameobject.setActive(true) || (false) hiccups game for 3 to 5 seconds
My game has 10 same type of objects. previously i was doing wrong that was enabling and disabling gameobjects by destroy() and instantiate() methods. This was also hiccups my game for too long time. so I searched game optimization and found that these methods are the reason. so I found solution that is to use of SetActive() function. But also this method hiccups my game too. My Game only hiccups when I activate 10 objects simultaneously by this ganeobject.setActive(true) method. I have created one parent object and all these 10 objects are its child objects. so here is one of the portion of my script.
public GameObject pins1, pins2;
void Update()
{
.....
.....
if(Count==1)
{
pins1.SetActive (true);
}
if(Count==2)
{
pins1.SetActive (true);
}
.....
.....
.....
.....
}
by making public Gameobject pins1, pins2, I assign that parent gameobject from inspector. so it will enable and disbale its 10 child objects. But that hiccups the game everytime when I enable and disbable this objects. Can anyone suggest what is the sollution of this hiccups? As I've said, it hiccups only the time when these gameobjects are activated. Any suggestion/solutions appreciated.
Do you have any scripts attatched to your 'pins' (or any of their children) that have either an OnEnable, Awake or Start which might take a long time? (Awake and start are called when an object is enabled/activated for the first time, OnEnable every time you enable/activate the object)
In any case, the Profiler might help you locate it, as a 5 seconds hickup should be clearly visible :p
Actually, Start is only called once during the lifetime of the script and needs to be manually invoked on re-enabling an object.
void OnEnable()
{
Start();
}
Dont forget that while Count==1 or 2 is true in the code above, SetActive(true) is spam$$anonymous$$g away in Update; although I've never tested if this would be an issue. I imagine you need to have disabled the object for OnEnable to fire. Again, not sure if this is the case. Testing required.
@meat5000 'Actually, Start is only called once during the lifetime of the script'
To clarify, that is what I said. Start is called the first frame when the object is enabled for the first time. So if the object starts disabled, Start will be called at the moment it is activated for the first time, meaning that this could cause the described hickup.
I have not used Start(), Awake(), OnEnable() etc functions in my script. pins1, pins2 are normal gameobjects(parent), each of them have 10 children gameobjects which are my pins and yes.... all of the child object have the same script attached... So I have 10 parent objects, each of them have 10 children, so total 100 gameobjects. and all 100 gameobjects have same script attached. and for enabling and disabling gameobjects, i have public GameObjects pin1,pins2... which are parent and assigning them in inspector of child for enabling and disabling. Read this twice if you dont get what am I saying because its complicated, I know.. Sorry For that !
Answer by kishsolanki · Jul 18, 2015 at 07:13 PM
I solved it by myself.. but thanks guys for your suggestion. the original problem for hiccups was by the mesh of the gamebject. my children gameobjects have mesh of 3138 vertices and 6272 triangles. so I was instantiating those 10 objects simultaneously and that causes the problem. So i edited that gameobject in blender and by the blender s/w, I reduced the number of vertices and triangles to 1386, 1588 respectively. That makes the run without hiccups !
Answer by Brascal · Jul 16, 2015 at 09:32 AM
Hello
Maka a switch (pins) case.
example switch (pins) { case pins1 = true: pins2 = false;}
Of course you do need also a function class to handle the changes.
public void PinsHandler() { // here make pins alive }
It Didn't work ! Same Situation. Hiccups when enabling these objects !
Try make a longer way. So you have a child script? $$anonymous$$ake then a core script where you call child. And then make a new scrip where you you handle child behave.
Example public class Child : Core
public overrited void SpinsHandler() {
public GameObject pins1, public GameObject pins2; }
Core call SpinHandler() { switch happend here. and you need declare inside () SpinHandler. }
Answer by Ibzy · Jul 16, 2015 at 11:40 AM
Would this not be hiccuping because you're calling it in update? What would be the main trigger for activating the pins?
Also, I would use a switch instead of many ifs.
switch (Count) {
case 1:
pins1.setActive(true);
break;
case 2:
pins1.setActive(true);
pins2.setActive(true);
break;
}
Now I've kept this pins' SetActive() in the switch. but nothing changed. lagging !!!!! and all this stuff now i've put it in the function outside the update and call this function in the update by
void FixedUpdate()
{
.....
.....
FunctionName();
.....
}
But nothing changed. still lagging.
Answer by RLin · Jul 16, 2015 at 11:45 AM
It could just be that your pins are too complex of objects. Depending on how often their active state is changed, you will always notice lag. Also, if many instances of the script are affecting many pins, you may want to stagger the setActive events so it does not all execute at the same time. If collision/physics does not matter, disabling the renderers only should not cause a hiccup.
I am activating pins (10 GameObjects) simultaneously. so at that time game lags for almost 4-7 seconds. I dont know what is the problem with my 3d gameobject(pins). It may be possible that activating them simultaneously is the reason of hiccup. but I have to do it. But dont know how to get rid of this hiccup at the time of activating !!