Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 StianH · Feb 01, 2013 at 09:37 PM · c#gameobjectswap

How to swap 2 objects place with each other.(c#)

Hey I have been looking for a way to swap 2 objects place with each other, but haven't found anything that does that. Lets say there is a board with 5x5 squares on it(the squares are 1x1). On every square there is a gameobject. What I want is when I click on one game object, I can then click on one of the others to swap their place with each other. If anyone knows of a tutorial that deals with this kind of stuff, please tell me.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by iwaldrop · Feb 01, 2013 at 09:54 PM

 Transform t = targetGameObject.transform;
 
 gameObjectToMove.transform.position = t.position;

or, on the GameObject,

 transform.position = targetTransform.position;
Comment
Add comment · Show 7 · 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
avatar image iwaldrop · Feb 01, 2013 at 10:24 PM 0
Share

All the above says is that you want to set the transform of a GameObject to another transform. This will instantly 'teleport' that object to that position, rotation, and scale. If you don't want to apply the rotation or scale, just use it's position (a Vector 3).

What you need to know if you want to do this is where the "other" object is and set your position to that other object. An object's position is stored in it's transform. So just get a reference to that other transform or, best case, have a controller handle moving things around.

 void $$anonymous$$ovePieceToLocation(GameObject objectTo$$anonymous$$ove, GameObject objectDestination, bool destroyDestinationObject)
 {
     // set position
     objectTo$$anonymous$$ove.transform.position = objectDestination.transform.position;

     // set rotation
     objectTo$$anonymous$$ove.transform.rotation = objectDestination.transform.rotation;

     // set scale
     objectTo$$anonymous$$ove.transform.scale = objectDestination.transform.scale;
     
     if(destroyDestinationObject)
         Destroy(objectDestination);
 }

All you're doing is handling the repositioning of one object to the old object's position and then optionally destroying the old one. You could also just turn off the old GameObject, or move the old one to the former's position (transposing them).

Let me know if you have more questions, and use the 'add new comment' button! :)

avatar image Wolfram · Feb 01, 2013 at 10:30 PM 1
Share

You cannot assign to transform. What you mean is transform.position, and if you need to take care of the orientation, too, similar for transform.rotation.

avatar image iwaldrop · Feb 01, 2013 at 10:35 PM 0
Share

Indeed, wolfram! I guess that explains why I never do it...

avatar image Wolfram · Feb 01, 2013 at 10:47 PM 0
Share

There is no transform.scale, you'll have to use transform.localScale ins$$anonymous$$d.

avatar image iwaldrop · Feb 01, 2013 at 11:02 PM 0
Share

lol...guess that's what I get for trying to answer and work at the same time!

Show more comments
avatar image
0

Answer by StianH · Feb 01, 2013 at 10:17 PM

I don't have much knowledge when it comes to scripting, if it isn't to much to ask, could you elaborate a little on that?

Comment
Add comment · Show 1 · 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
avatar image iwaldrop · Feb 01, 2013 at 10:18 PM 1
Share

Sure. But, first, you commented using the answer field. Please, in the future, use the add a new comment button where you're trying to comment. I'll elaborate there.

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.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How to swap 2 game objects from any position on a 2d game pad? 0 Answers

Distribute terrain in zones 3 Answers

GameObject as a constraint for generic class 1 Answer

How can I add the OnTriggerEnter function to all game objects that I instantiate? 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