- Home /
How do I reset a objects position through a GUI Button ?
Hi being a unity rookie still, could someone please tell me how I possible get an object on my game screen to reset back to it's starting position after it has been dragged around everywhere, by click a button through a
function OnGUI () {
if (GUI.Button (Rect (10,10,50,25), "Reset")) {
print ("Resetting original position");
}
}
(This is the code I have at the moment)
Answer by duck · Nov 19, 2010 at 02:00 PM
You have to store the original position & rotation of the object in variables, when the script starts up. Then you can re-apply these values in your GUI Button code, like this:
var startPos : Vector3; var startRot : Quaternion;
function Start() { startPos = transform.position; startRot = transform.rotation; }
function OnGUI () { if (GUI.Button (Rect (10,10,50,25), "Reset")) { print ("Resetting original position"); transform.position = startPos; transform.rotation = startRot; } }
You're welcome. Please remember to accept and upvote answers which solve your problem!
Your answer
![](https://koobas.hobune.stream/wayback/20220613083709im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cannot click UI button. 4 Answers
calling OnGUI events are cancelled by LateUpdate? 0 Answers
Timed particle system disable 0 Answers
Change button texture when its clicked 1 Answer
Reset button 1 Answer