- Home /
Resetting an object when returning to an object pool?
I am using an object pool for efficiency but have only started implementing it, I was just Instantiating before.
I have my pool set up and the enemies enable and disable correctly but its causing some positioning issues when playing some of my animations.
My scene consists of an enemy that will walk toward me and when a certain distance away, start shooting from his mouth. When I attack him, the death animation plays which involves him going on his back and returning to the pool.
When I enable this enemy again, he walks towards me as normal but when he reaches his distance away and starts shooting, its like he's shooting from his back, the bullet still goes towards me but the animation is playing incorrectly.
I was just wondering if there was a way I could completely reset an object when it goes into the pool? And when it is enabled again, it would be like it was just freshly instantiated?
TL;DR
I want to reset my enemy object to its default settings when it returns to the object pool as if it was just instantiated as new.
Theres no built in method, so its a case of setting up a function to save and set the state, then just save the state of the prefab object, and set your objects to that at runtime when it enters the pool. Unfortunately if you need to do that dynamically, i.e. without knowing all the variables, then you will probably need to use some kind of reflection. If you know all the variables that need to be reset, it's a lot simpler!
Of course the easier method, is just to solve your positioning bug through positioning...
Answer by MotoSV · Sep 08, 2015 at 05:59 PM
For the object pool I'm using when I want to recycle an existing instance from the pool I first pass it into the following method...
private void PrepareClone(GameObject instance, Vector3 position, Quaternion? rotation, Transform parent, bool isActive)
{
instance.transform.position = position;
instance.transform.rotation = rotation ?? instance.transform.rotation;
instance.transform.SetParent(parent ?? this.transform, true);
instance.SetActive(isActive);
}
...and then I execute this line...
instance.SendMessage("OnSpawn", this, SendMessageOptions.DontRequireReceiver);
So, the first part will set the position, rotation, etc, and then I send a message to the instance which then allows that instance, or more specifically each component on that instance, to perform whatever initialisation is required to get it ready for use. As a side note, the this
being passed along with the message to the instance is a reference to the pool that is managing it.
Now I don't know if this will solve the animation issue (probably not), but it will allow an object to be reset when retrieved from the pool.
Your answer

Follow this Question
Related Questions
Change value of a projectile that is instantiated in Awake and pooled 1 Answer
Checking if object intersects? 1 Answer
Strange cloning/ghosting problem with instantiated particles 0 Answers
Variable not being saved? 0 Answers
Object pooling? 1 Answer