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 /
This question was closed Oct 16, 2014 at 01:30 PM by RedDevil for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by RedDevil · Mar 03, 2014 at 10:01 AM · transformpositionswapswapping

Swapping gameobjects

I am trying to make a candy crush type game and im looking at changing the position of 2 game objects.What im trying to do is to click a object then click the target object to change the 2 object position between each other.The problem im curently facing is that i can change the target position but the original selected object remains the same.Basicly i can change the target only and not the first object. Here is my code:

  if (Input.GetButtonDown ("Fire1")) 
         {
             Ray ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
             if (Physics.Raycast (ray1, out hit, Mathf.Infinity)) 
             {
                 switch (clickCount) 
                 { 
                 case 2:
                 case 0: 
                         originalTile = hit.collider.gameObject;
                         clickCount = 1;
                         break;
                 case 1: 
                         targetTile = hit.collider.gameObject;
                         clickCount = 2;
                         break;
                 }
                 Swap();
             }
         }
     }
 }

 void Swap()
 {
     var temp = originalTile.transform.position;
     originalTile.transform.position = targetTile.transform.position;
     targetTile.transform.position = temp;
 }
Comment
Add comment · Show 2
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 RedDevil · Mar 03, 2014 at 10:11 AM 0
Share

i also keep getting the error that the target tile is not assigned.

avatar image RedDevil · Mar 03, 2014 at 11:41 AM 0
Share

i solved it myself the problem was with the position of the Swap call.i inserted it into one of the cases and it worked just like i wanted too.

2 Replies

  • Sort: 
avatar image
0

Answer by SunnySunshine · Mar 03, 2014 at 10:12 AM

You probably need to create a new Vector3. Try this:

 void Swap()
 {
     var temp:Vector3 = new Vector3(originalTile.transform.position.x, originalTile.transform.position.y, originalTile.transform.position.z);
     originalTile.transform.position = targetTile.transform.position;
     targetTile.transform.position = temp;
 }
Comment
Add comment · Show 2 · 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 RedDevil · Mar 03, 2014 at 10:15 AM 0
Share

im getting exactly the same error.Says the original tile was not assigned.seems like its exactly the same

avatar image RedDevil · Mar 03, 2014 at 10:16 AM 0
Share

it is swaping them but the problem im getting is that i cannot asign a new original tile.

avatar image
0
Wiki

Answer by Kajal · Sep 20, 2014 at 12:49 PM

try this...it's work for me.Attach this script to your Main Camera.

 using UnityEngine;
 using System.Collections;
 
 
 public class planeMove : MonoBehaviour {
     
     
     int click1 =1;
     GameObject originalTile;
     GameObject targetTile;
     bool t2 = true;
     bool t1 = true;
     
     
 void Update () {
         
             if(Input.GetMouseButtonDown(0))
             {
             Ray ray=Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if(Physics.Raycast(ray,out hit, Mathf.Infinity))
             {
                 switch(click1)
                 { 
               
                 case 2: 
                        originalTile = hit.collider.gameObject;
  
                         click1= 1;
                         t1 =false;
                         break;
                 case 1: 
                           targetTile = hit.collider.gameObject;
                          click1 = 2;
                         t2 = false;
                         break;
                 }
                 
                 if(t1 == false && t2 == false)
                     Swap(originalTile, targetTile);
             }
         }
     }
            
 
     void  Swap(GameObject org, GameObject tar)
     {
         Vector3 temp =  new Vector3(org.transform.position.x, org.transform.position.y, org.transform.position.z);
         org.transform.position = tar.transform.position;
         tar.transform.position= temp;
         t1 = true;
         t2 = true;
     }
 }

maybe this will help you kajal

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

Follow this Question

Answers Answers and Comments

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

Related Questions

Swap Object Positions 3 Answers

Need enemy to follow only active character 0 Answers

Drawing a (debug) line between anchored UI element and mouse? 0 Answers

I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers

How to move an object to your current positon? 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