• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sitrane · Dec 06, 2011 at 06:42 PM · collisionpositionreset-positiontransformation

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dannyskim · Dec 06, 2011 at 06:58 PM 0
Share

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.

avatar image Sitrane · Dec 06, 2011 at 07:36 PM 0
Share

Ok that makes sense,

How would I go about doing that?

avatar image Rabwin · Dec 07, 2011 at 12:35 AM 0
Share

You can't embed code in a comment, do it in an answer since this would count as one and finish this question.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

My character's y position gets reset on play. 0 Answers

Detecting Collision points of particles? 1 Answer

How to get the relative position of a Collider with OnControllerColliderHit()? 2 Answers

how to make my enemy stop walking? 1 Answer

Moving object with transform.position ignore other objects even if they collided 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges