- Home /
How to reset a prefab used in object pool ?
Hi,
I am using an object pool to avoid using instantiate for performance reasons on mobile. I created a prefab of a banana peel using joints and rigidbodies to make the banana peel look realistic when falling from the tree. Because there area lot of banana peels that fall and these are activated and deactivated from the object pool I wanted to reset the prefab each time it falls down. When the object touches the ground it is deactivated and activated back to the spawn point. When I use Instantiate and Destroy the prefab is reset each time but it's not when using object pool. I know that i can reset the rigidbodies velocity and angular velocity but still it doesnt reset the prefab at its original prefab state. Is there a way how to reset the prefab state ?
Thank you, Gerald.
Answer by HawkSandwich · Jun 17, 2017 at 04:25 PM
Can't you store the default position/rotation/etc of the object once you first instantiate it, then each time it "respawns", just throw those values at it?
This is what I am doing and it doesn't work. $$anonymous$$aybe it has to do with joints that i am using for the banana peel in order to look realistic when colliding. $$anonymous$$aybe Unity should add a function to reset prefabs if there isn't one already.
What exactly do you mean by "original prefab state"? Do you have a script attached and are you trying to reset the values on this? Or are you trying to reset position/rotation etc? Also where are you calling this from? You have to call from OnEnable().
For example, I had bullet pool. Each bullet would have a script called bullet.cs. I would also have a method in it called ResetBullet(). When I am done with the bullet, I will call, on, OnDisable(), ResetBullet(). In this method I will have everything I need.
Also, if you are trying to reset the transforms back to original(e.g. vector3.zero), by other means than using physics forces, you may have to set ALL rigidbodies to $$anonymous$$inematic (Is$$anonymous$$inematic =true). Then turn it back off.
By original prefab state I mean the state that the prefab is before being used in the pool. Object pooling simply instantiates the prefab once and then it just activates it and deactivates it when i want. Also, I do have a reset function that resets the position and the rotation inlcuding velocity, but that doesn't solve the problem because the prefab that I am using (banana peel) does have some hinge and configurable joints. $$anonymous$$aybe it is the joints that do not reset like they are at the beginning before the prefab is instantiated. I guess that the best thing would be to reset the prefab but don't know how to because resetting a prefab isn't a thing yet in Unity.
Answer by eagleTiger · Aug 08, 2017 at 10:44 AM
Try PrefabManager or PrefabManager, a useful plugin for pooling prefabs
Use its public functions to spawn, recycle and destroy. And to reset any class attached to it using generic functions specifying the component and reset accordingly.
Hope it solves your problem!
Your answer
Follow this Question
Related Questions
Object Pooling Script for Autorunner? 1 Answer
Reverting an object with children to its original prefab-state on runtime. 0 Answers
gameObject.SetActive(false); not working as intended 3 Answers
Constructing an object from prefabs 0 Answers
Avoid getting same object as previous one with Random.Range? 2 Answers