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 /
  • Help Room /
avatar image
0
Question by freemann098 · Sep 14, 2015 at 06:17 PM · c#gameobjects

How to get what was the previous GameObject?

I'm working on a game where one of the gameplay elements involves moving around three circles to match some other colors.alt text An example is the picture above. I have already implemented dragging the circles around but if I were to drag red over green, green will be under red, I want that green Game Object to be where the empty space is ( so basically switch spaces ). I have set up a way on how it knows what position to put this object but it doesn't know what object. That's my question, how do I detect what gameobject to switch into that empty space. This is the script attached to those circle Game Objects

 using UnityEngine;
 using System.Collections;
 
 public class selectedCircle : MonoBehaviour {
 
     int isSelected;
 
     Vector3 screenPoint;
     Vector3 offset;
     Vector3 posotionForPrev;
 
     Collider2D posLeft, posMid, posRight;
     bool leftHas, midHas, rightHas;
     
 
     void Start()
     {
         leftHas = true;
         midHas = true;
         rightHas = true;
 
         PlayerPrefs.SetInt("isSelected", 0);
         isSelected = PlayerPrefs.GetInt("isSelected", 0);
 
         posLeft = GameObject.Find("posLeft").GetComponent<Collider2D>() ;
         posMid = GameObject.Find("posMid").GetComponent<Collider2D>();
         posRight = GameObject.Find("posRight").GetComponent<Collider2D>();
         
     }
 
     void Update()
     {
        checkFreeSpot();
     }
 
     void OnMouseDown()
     {
         screenPoint = Camera.main.WorldToScreenPoint(transform.position);
 
 
         offset = transform.position - Camera.main.ScreenToWorldPoint(
             new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
 
     }
 
 
     void OnMouseDrag()
     {
         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
 
 
         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
         transform.position = new Vector3(curPosition.x, transform.position.y, 0);
 
     }
 
     void OnMouseUp()
     {
         //these if statements used to lock in object to position
         if (posLeft.bounds.Contains(gameObject.transform.position))
         {
             gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(-1.94f, -4.11f, 0), 5) ;
         }
         else
         {
             leftHas = false;
         }
         if (posMid.bounds.Contains(gameObject.transform.position))
         {
             gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(0.21f, -4.11f, 0), 5);
         }
         else
         {
             midHas = false;
         }
         if (posRight.bounds.Contains(gameObject.transform.position))
         {
             gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(2.22f, -4.11f, 0), 5);
         }
         else
         {
             rightHas = false;
         }
     }
 
     void checkFreeSpot()
     {
         if (leftHas == false)
         {
             posotionForPrev = new Vector3(-1.94f, -4.11f, 0);
         }
         else if (midHas == false)
         {
             posotionForPrev = new Vector3(0.21f, -4.11f, 0);
         }
         else if (rightHas == false)
         {
             posotionForPrev = new Vector3(2.22f, -4.11f, 0);
         }
     }
 }
 


untitled.png (7.4 kB)
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
1
Best Answer

Answer by GreenCore · Sep 14, 2015 at 07:47 PM

Hi!

Are you naming your game objects anything special? Or are you just going with some generic name for all the circles (e.g. circle1, circle2...).

The simplest way I can think of right now, in case you are not using the game object's name for anything else, is to name them after their position in the game world, like this:

 void Start(){
         gameObject.name = transform.position.ToString();
 }

Now, when the user releases the mouse button, you need to do two things:

-Find the game object named after your new position and move it to your previous position.

-Rename both objects to their new positions.

 void OnMouseUp() {        
     GameObject other = GameObject.Find(transform.position.ToString());
     if(other != null){
         other.transform.position = prevPosition;
         other.name = prevPosition.ToString();
     }
     gameObject.name = transform.position.ToString();
 }

Hope it helps. Good luck!

Comment
Add comment · Show 4 · 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 freemann098 · Sep 15, 2015 at 06:05 PM 0
Share

This seems to be slightly working, when I switch the objects they don't overlap but they don't switch into the other spot but into the center of the screen. I believe this is happening because my system for getting the position of where it should be is not working correctly.Any ideas on fixing this or a better system for getting the position needed?

avatar image GreenCore freemann098 · Sep 15, 2015 at 06:48 PM 1
Share

How did you integrate the examples I wrote into your code? $$anonymous$$aybe try setting the value for prevPosition inside On$$anonymous$$ouseDown().

Note that prevPosition (in my example) is not supposed to be the same variable as posotionForPrev. Not sure what you are using that one for, but in your original post it seems like you are only assigning it a value inside CheckFreeSpot() but never actually using it.

avatar image freemann098 GreenCore · Sep 15, 2015 at 07:06 PM 0
Share

Thanks for the answer, I got it to work using your suggestion to set prevPosition in On$$anonymous$$ouseDown()

Show more comments

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

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

Related Questions

How to toggle elements within a prefab in Unity 0 Answers

[solved] Survival Shooter - 'Player' tag mysteriously filled 2 Answers

Referencing uninstantiated GameObject within a class? 0 Answers

How to create a child object inside the parent (script) and assign independent variables? 0 Answers

The pickups aren't rotating on the spot, but around the screen. My player game object also isn't receiving input from my keyboard. What is going on? 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