Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ahmedre · Jul 29, 2016 at 06:57 PM · gameobjectclickswapswapping

How to swap 2 gameobjects after 2 click!

alt text

my test:

 var hit : RaycastHit;
  
 var noObj : Transform = null;
 var switchObj : Transform = null;
 var tempObj : Transform;
  
 function Update()
 {
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    
     if (Input.GetMouseButtonDown (0))
     {
        
         if(!noObj)//no object picked yet
         {
             if (Physics.Raycast (ray, hit, 100))
             {
                 noObj = hit.transform;//save picked objects transform
                 tempObj.transform.position = noObj.transform.position;
                
             }
             
         }
            
         if(noObj !=null)//if noObject now has a transform
         {
             if (Physics.Raycast (ray, hit, 100))
             {
                 switchObj = hit.transform;
     
                 DoTheSwitch();
             }
         }
     }
 }  
  
 function DoTheSwitch()
 {
     noObj.transform.position = switchObj.transform.position;//moves the first clicked object to the second clicke objects position
     switchObj.transform.position = tempObj.transform.position;
 }


dfs.png (49.4 kB)
Comment
Add comment · Show 8
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 josehzz112 · Jul 30, 2016 at 12:29 AM 0
Share

It looks good to me, could you add more info like, the scene setup, or add some debug lines to see if Physics.Raycast is true, and what are your results with this test?

avatar image ahmedre josehzz112 · Jul 30, 2016 at 11:26 AM 0
Share

i used a 2d gameobjects (sprite) alt text

qds.png (112.1 kB)
avatar image Sethhalocat · Jul 30, 2016 at 12:27 PM 0
Share

maybe you can do it through animation

avatar image ahmedre Sethhalocat · Jul 30, 2016 at 12:34 PM 0
Share

That's what I wanted to do, but how can i detect these two sprites ? :D

avatar image Sethhalocat ahmedre · Jul 30, 2016 at 08:58 PM 1
Share

Perameters are a good feature, you can do an on click feature that adds one to a float or int, and have the animation work when it = 1 and the second when it = 2...

avatar image ahmedre · Jul 31, 2016 at 09:46 AM 0
Share

Now, with 3D objects test i have this problem .. one of object swapping but the second Remained in place alt text

3dfaza.png (14.6 kB)
avatar image ScaniX ahmedre · Jul 31, 2016 at 10:13 AM 0
Share

Did you change the type of tempObj to Vector3 (from Transform) and used it like in the example in my answer?

 tempObj = noObj.transform.position


avatar image ahmedre ScaniX · Aug 01, 2016 at 10:01 AM 0
Share

Yes it's work fine .. thx :)

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ScaniX · Jul 30, 2016 at 09:04 PM

I think three things are missing.

1) an else between your ifs, so you are not using the clicked object as first AND second object.

2) clearing noObj after the switch for the next click

3) tmpObj should be a Vector3

  function Update()
  {
      var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     
      if (Input.GetMouseButtonDown (0) && Physics.Raycast (ray, hit, 100))
      {
          if(!noObj)//no object picked yet
          {
              noObj = hit.transform;//save picked objects transform
              tempObj = noObj.transform.position;
          } else if (noObj !=null) { //if noObject now has a transform
              switchObj = hit.transform;
              DoTheSwitch();
          }
      }
  }  
   
  function DoTheSwitch()
  {
      noObj.transform.position = switchObj.transform.position;//moves the first clicked object to the second clicke objects position
      switchObj.transform.position = tempObj;
      noObj = null;
  }

(I also shortened it a bit, but that doesn't change the logic)

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 ahmedre · Jul 30, 2016 at 09:38 PM 0
Share

Thx $$anonymous$$an .. Finally It Works :)

avatar image
0

Answer by cdnDave · Jul 29, 2016 at 09:16 PM

What happens when you run your test code? It looks like you are missing a line in your DoTheSwitch function to assign the noObj position to tempObj before moving it. I think it needs to be

  function DoTheSwitch()
  {
      tempObj.transform.position = noObj.transform.position; //Cache the position of noObj before moving it 
      noObj.transform.position = switchObj.transform.position;//moves the first clicked object to the second clicke objects position
      switchObj.transform.position = tempObj.transform.position; //move the second object to the cached position of the first object 
  }

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 ahmedre · Jul 29, 2016 at 11:34 PM 0
Share

Not working :/ my game is 2D game and when i click on one of gameobjects it display this msg " pick one of these objects "

avatar image
0

Answer by ToasterKyle · Jul 31, 2016 at 11:32 AM

It is because the tempObj Transform object is never created, you need to either change tempObj to Vector3 or Instantiate the tempObj before setting the position.

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 ScaniX · Jul 31, 2016 at 11:50 AM 0
Share

As already stated in my answer. As you cannot really instantiate a transform, you should just use a Vector3.

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

9 People are following this question.

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

Related Questions

Quad Objects not responding to clicks. 1 Answer

On Button Click Enable - Disable GameObject Through Inspector 1 Answer

[Edited]Swap position of two Objects for Android on touch 2 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

Rotate Clicked Gameobject / Perhab 0 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