- Home /
Resetting multiple objects position
I found this code linked for resetting an objects position and then modified it to reset the score for my scene but when I apply the script to my multiple objects, it stops working correctly.
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")) {
transform.position = startPos;
transform.rotation = startRot;
CollisionSystem.myScore = 0;
}
}
I'm not entirely sure what I need to modify to make it work correctly.
Any help would be greatly appreciated.
Well, if you're attaching this script to multiple objects in your scene, then they're all going to be drawing a GUI button that says reset. So if you have ten objects and ten instances of the script, there are going to be ten buttons that are in the exact same position. You probably want this script on an empty game object that references the multiple objects in your scene, stores them in an array, and loops through that array resetting their positions.
You can't embed code in a comment, do it in an answer since this would count as one and finish this question.
Answer by funkyllama · Dec 07, 2011 at 01:11 AM
How about something like this:
Create an empty game object & attach the following script to it. Then in the inspector resize the array "myObjects" depending on how many you have & fill the list with you objects. I'm a bit of a noob too so this maybe could be done better, but it'll work. Hope it helps :)
//
var myObjects : GameObject[];
private var startPos : Vector3[];
private var startRot : Quaternion[];
function Start() {
// resize arrays to match objects array
startPos = new Vector3[myObjects.length];
startRot = new Quaternion[myObjects.length];
for (i = 0; i <= myObjects.length - 1; i++) {
// fill position & rotation arrays for each object in objects array
startPos[i] = myObjects[i].transform.position;
startRot[i] = myObjects[i].transform.rotation;
}
}
function OnGUI(){
for (i = 0; i <= myObjects.length - 1; i++) {
//create a button for each of our
objects in the array & offset position x
if (GUI.Button (Rect (10 + (i * 55), 70, 50, 25), myObjects[i].name)) {
// reset object related to this button
myObjects[i].transform.position = startPos[i];
myObjects[i].transform.rotation = startRot[i];
//CollisionSystem.myScore = 0;
}
}
}
//
Here is a small project if you'd like to see it working :)
ResetObjects.zip
Your answer
![](https://koobas.hobune.stream/wayback/20220613061837im_/https://answers.unity.com/themes/thub/images/avi.jpg)