• 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 tanmayR · May 27, 2013 at 05:16 AM · swap

how to swap 2 gameobjects

hi

My ques is that i wanted to swap 2 gameobjects when i click on either of 1 but the problem is that when i click on the object all other gameobjects also get swap ......here is my code plz help me

 var  RedPrefab  :GameObject;
 
 var BluePrefab :GameObject;
 
 var EmptyPrefab :GameObject;
 
 var temp =GameObject;
 
 var tr :boolean;
 
 
 function Start ()
 {
  
     tr =false;
 }
 
 function Update () 
 {    
   
        var distance1 = Vector3.Distance(RedPrefab.transform.position,GreenPrefab.transform.position);                  
                 
         if(Input.GetMouseButtonDown(0))
               {
                         tr =true;
                          //EmptyPrefab =GreenPrefab.transform.position;
                        //GreenPrefab.transform.position = RedPrefab.transform.position;
                        //RedPrefab.transform.position = EmptyPrefab;                                                     
                        var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                      var hit : RaycastHit;         
                       if (collider.Raycast (ray, hit, 100.0)) 
                         {                    
                                                                    Debug.Log("fg");           
                             temp=gameObject.transform.position;
                             
                                                      
                             }                                              
  
              }
 }
 function OnCollisionStay(col:Collision) 
                             {
                                 if(tr==true)
                                 {
                                //if(col.gameObject.tag == "Blue") 
                                       // {
                                       //  gameObject.transform.position =BluePrefab.transform.position;
                                        // BluePrefab.transform.position=gameObject.transform.position;
                                          
                                      //  }
                                  }
                             }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ashkan_gc · May 27, 2013 at 05:37 AM

Who this script is attached to. A simple code to swap a prefab with another is like this, var otherPrefab : GameObject

 function OnMouseDown()
 {
 var temp = transform.position;
 transform.position = otherPrefab.transform.position;
 otherPrefab.transform.position = temp;
 //You can change references if you want as well.
 }
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 tanmayR · May 27, 2013 at 06:44 AM 0
Share

thanks for the answer but what i want to say is that there are many references to the prefab and i want that if i click on the specific object then only that cube get swap with the cube which is adjacent to that.....its like a jewel game in which 3 cubes are there of different color and i want to swap the specific color of cube onto which i click with the adjacent one......

thankz in advance....

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

14 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 avatar image avatar image avatar image

Related Questions

Swapping lightmaps during runtime? 1 Answer

Swap Object Positions 3 Answers

Swapping between Diffuse and Transparent/Diffuse Shaders 0 Answers

hi, Im new in unity and I'm searching a way to swap two different 2d gameobjects on the same position when its clicked the object 1 to obeject 2.? ,hi guys, 1 Answer

Networking - Camera Swapping to Target Incorrect Player 2 Answers


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