- Home /
Reverting an object with children to its original prefab-state on runtime.
Hey!
I'm using an object pool for my instantiated prefabs so, before returning my used object to the pool I need it to be reset to its original prefab state.
My case is:
Parent - Child
The child gets detached during runtime, and needs to be reverted to its original state on runtime, before being returned to the pool.
Hope you can help!
EDIT (Partly Sovled)
So, this is what I have for now. It works as I want it to, but I still feel as if it's not the right way to do it.
// Prefabs in the object pool.
public GameObject[] prefabs;
// List of prefab childs.
private List<Transform>[] prefabReferences;
public void ResetObject(GameObject prefab)
{
for (int i = 0; i < prefabs.Length; i++)
{
GameObject tmpPrefab = prefabs[i];
if (tmpPrefab.name == prefab.name)
{
for (int j = 0; j < tmpPrefab.transform.childCount; j++)
{
prefab.transform.GetChild(j).transform.localPosition = prefabReferences[i][j].localPosition;
}
}
}
}
Any comments on this are grealy appriciated!
An instantiated object at runtime has no built-in metadata that lets it know about its original prefab! You'll have to do something yourself. While this can be done in the editor, it relies on the existing 'prefab/instance' framework that gets broken at runtime.
Okay, this helps a lot. I'll try working on something to work around it.
Appriciate it! :)
(For anyone: If someone have some code examples or experienced a similar problem I would appreciate the help)
Your answer
![](https://koobas.hobune.stream/wayback/20220613114402im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to reset a prefab used in object pool ? 2 Answers
How do I reset a script's values at run time to what they were at start? 3 Answers
Script on Prefab is not changeable without changing all other instaniated objects 1 Answer
More than one animation associated with an object 1 Answer
Why prefab revert does not work for position and rotation? 1 Answer