Revert complex prefab structure (transform and enabled status of each gameobject child)
Hello, I would like to ask: Given a prefab with many (random, always different, quantity) children, the player is able to interact with anyone of them changing it's transform (position, rotation and scale) and/or disabling them.
Is it possible to "revert" the status of all the children, thus restoring the tranform and enable state of each gameobject (without destroying and re-instantiating the same prefab)?
Thank you
Answer by Cameron_SM · Feb 22, 2017 at 08:15 AM
@noblex Of course it's possible, but there's no built in method for this.
The problem you're trying to solve here is reverting the state of an object to a previous state. In order to do that, you will need to have a record/copy of the state you wish to revert to.
For a transform that could be a simple as the following:
public class Revertable : MonoBehaviour
{
private Vector3 revertPosition;
private Quaternion revertRotation;
private void Awake()
{
revertPosition = transform.position;
revertRotation = transform.rotation;
}
public void RevertTransform()
{
transform.position = revertPosition;
transform.rotation = revertRotation;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612111645im_/https://answers.unity.com/themes/thub/images/avi.jpg)