- Home /
Removing dead bodies, big lag issue.
'Ok so as I said I am having lag issues because bodies are building up causing lag, can anyone look at this script and tell me what Im doing wrong? and if possible fix it for me? explain it also if you fix it? thanks!'`
var body : GameObject; ---Is supposed to tell that the parent of the script will remove
setTimeout(cleandeadbodies(),4000); ---a timer before the function function cleandeadbodies(){ ---the function body.Parent = nil; ---deletes the ragdoll/dead body }`
Actually one thing you could do to avoid lagging is to create a pool of object. It takes a little thinking to implement but the result is worth it.
You create an array of objects with in your case, the objects and the ragdolls. Ins$$anonymous$$d of instatiating and destroying objects you simply deactivate them. Instantiatiation takes a little computation but no big deal while destroying is the harmful one. It takes the garbage collector to check what is used in the memory and what is free. Also, (to be verified I do not know the internal memory management of Unity) it can create fragmentation, which is when memory has unoccupied allocations that are too small to be used.
As pseudo code your pooling could go like this:
-Instantiate 20 enemies in the Start function and place them in an array. Instantiate 20 ragdolls and place them after (index 20-39)
-Now you can activate/deactivate simply keeping track of the index. For instance, array[15] gets killed:
array[i].active = false;
array[i+20].active = true;
I just remove the objetc and replaced it with ragdolls. Obviously you would have to position it and so on. But here is the idea.
Answer by fafase · Oct 07, 2012 at 06:48 AM
The simple way is to destroy them right after instantiation.
var obj=Instantiation(prefab, position,rotation);
Destroy(obj,20.0f);
Here you create an object (whatever it is), then you place a reference that you use to destroy it.
The second parameter of destroy is a timer. In my example, the object is removed after 20s.
This is really good but does it get attached to the ragdoll and as soon as its spawned gets activated?
I do not get what you mean. The reference is just a variable that refers to the actual variable (it refers to...). In my example, obj is a reference to the object instantiated so that you can access it and apply all kind of actions. In this case, we tell the compiler to destroy the obj object after 20s. As the reference exists in this scope, it works. For instance:
if(something){
var obj=Instantiation(prefab, position,rotation);
}
Destroy(obj,20.0f);
will not work because the obj reference is declared inside the if statement and gets destroyed after }. Still the object will leave. Hopefully, I make sense...
Well then how do I make it so it will cleanup the dead ragdoll body 4 seconds after the ragdoll spawns?
Answer by BlackArcane · Oct 07, 2012 at 06:46 AM
You could try having a master object that will check your scene for dead bodies (using a tag) and removing them after set intervals. Good luck! :)