- Home /
Reset Object
Is there a more stream lined way of doing an object reset or state restore ?
I am tired of writing the original position / original scale / original rotation in the onstart, and having a custom script on hundreds of objects just to reset them as they were when the level started.
Does anyone have a better strategy or is this the way to go ? It would be nice if the full transform had a setter for the transform.
At least then its 1 struct as opposed to 3 to have to set
Well, you could always write a 'copy' extension method for Transform...
Answer by sneftel · Jun 06, 2011 at 07:46 PM
You can use Application.LoadLevel()
to reload the scene in its original state.
Answer by GoSuNeem · Oct 31, 2011 at 04:21 AM
If you are ok with reloading the level just do what Ben 12 said.
Otherwise, could just have 3 vec3s and store their own default positions.
private Vector3 defPos;
private Vector3 defRot;
private Vector3 defScale;
void Start()
{
defPos = transform.position;
defRot = transform.localRotation;
defScale = transform.localScale;
}
public void Reset()
{
//Reset them back
}
That's the most simplest way imo.
Edit: Assuming if they are already placed in the world.
If you have several arrayed object, in different arrays, you are not storing their default values in this Start function,Aren't you? In this case you are just storing the last one.
Answer by syclamoth · Oct 31, 2011 at 04:46 AM
Just putting this out there-
public static void Apply(this Transform target, Transform original)
{
target.parent = original.parent.
target.position = original.position;
target.localScale = original.localScale;
target.rotation = original.rotation;
}
C# only, unless there's some way of doing this in JS. Basically you call it using
transform.Apply(some other transform);
and it'll set all of your transform's values to that of the other one.
Your answer
Follow this Question
Related Questions
how to reset a transform.rotate 1 Answer
Resetting a gameobjects position? 1 Answer
switch controlling transform 0 Answers
Rest Rotation Constraint 1 Answer