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 Asenderov · Jan 15, 2020 at 05:46 PM · 2d2d gamedrag-and-dropfindgameobjectswithtagfinding

Issues with dropping an object on multiple objects separetly - Simple 2d Drag & Drop

Hello,

What I want to achieve is: Drag and Drop the circle in the Start Screenshot on the squares separetly, so you'd drag and drop it on any square, and that square will be destroied, then continue to the other square after the circle is reset to the initial position.

The issue: The issue I have is that I have to drag the circle on the squares in order of the newest square created, so when I drag and drop on sq3>sq2>sq1 "sq3 is the last one created, and it's on the far right, sq2 is in the middle, sq1 is on the left", it doesn't show any errors, but when I drag and drop on sq2>sq1 then try to drag and drop on sq3, it shows an error of "Object reference not set to an instance of an object" as in the error screenshot, I've tried to findbygameobjecttag, but it also does the same thing, so I though the find alone will look for the name, since the find by tag looks for sorting order as well.

My Questions:

1- Is there a way I can make it disregard the order of the last created stack/list, where I can drag and drop the circle to any of the squares without any order?

2- Is there a way to attach the destroy code to the squares instead of having it in the circle code?

I would love if I can detect the drop of the circle from the square code, I know the OnCollisionEnter function, it works really good, but it detects the collision, but instead, I would love to detect the drop of another object on it, something like the OnMouseUp.

Below is the code I have attached to the circle, and it is the only code I'm using.

 private Vector2 initialPosition;

 private Vector2 mousePosition;

 private float deltaX, deltaY;



 void Start()
 {
     initialPosition = transform.position;
 }
 private void OnMouseDown()
 {

         deltaX = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
         deltaY = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
     
 }


 private void OnMouseDrag()
 {
         mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         transform.position = new Vector2(mousePosition.x - deltaX, mousePosition.y - deltaY);
 
 }


 private void OnMouseUp()
 {

     
     if (Mathf.Abs(transform.position.x - GameObject.Find("sq1").transform.position.x) <= 3f &&
        Mathf.Abs(transform.position.y - GameObject.Find("sq1").transform.position.y) <= 3f)
     {
         Destroy(GameObject.Find("sq1"));
         transform.position = initialPosition;
     }
     else if (Mathf.Abs(transform.position.x - GameObject.Find("sq2").transform.position.x) <= 3f &&
        Mathf.Abs(transform.position.y - GameObject.Find("sq2").transform.position.y) <= 3f)
     {
         Destroy(GameObject.Find("sq2"));
         transform.position = initialPosition;
     }
     else if (Mathf.Abs(transform.position.x - GameObject.Find("sq3").transform.position.x) <= 3f &&
        Mathf.Abs(transform.position.y - GameObject.Find("sq3").transform.position.y) <= 3f)
     {
         Destroy(GameObject.Find("sq3"));
         transform.position = initialPosition;
     }
     else
     {
         Debug.Log("Error");
     }

 }

Please note that I'm still a begginer, and I don't know a lot in C# coding, specially raycasting and all that stuff, so please if you recommend using any other type of code than the one above, please provide me with some information and examples if possible.

Many thanks!!!

error.png (47.1 kB)
start.png (5.7 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

0 Replies

· Add your reply
  • Sort: 

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

327 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 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 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 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 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 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 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 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 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 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 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 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

Moveable Button that resets when click is over 0 Answers

KillPlayer, Null Reference Exception. 2 Answers

Problem with Strings updatings on Gui 0 Answers

,2d gameObject unwanted outline 0 Answers

how to make my 2d top down character face the direction of the cursor 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