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 /
  • Help Room /
avatar image
0
Question by dbyers · Feb 06, 2021 at 01:55 PM · 2ddragdrag-and-dropdrop

2D Drag and Drop, but not when slot is already full

Hi,

First time asking for help, I'm very new to Unity but I've managed to create 90% of a game so far. I'm on the last puzzle and I can't get my head around this issue.

Basically, I have 35 objects and 3 slots to drop the objects on to. If there is already an object in a slot, it should be considered that the slot is busy and if the user tries to drop a second object onto an occupied slot, it should snap back to it's original location. The script I have deals with the drag and drop and the snapping etc, but I can't figure out how to stop the user dropping multiple items into the same slot!

Here's a screenshot, the code follows: alt text

Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DragViles : MonoBehaviour
 {
     private bool isDragging;
     bool isInSection1, isInSection2, isInSection3, isInShelfArea, section1busy, section2busy, section3busy;
     float old_z;
     bool section1Full = false;
     public void OnMouseDown()
     {
         old_z = this.transform.position.z;
         this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, -1.671000f);
         isDragging = true;
 
     }
 
     public void OnMouseUp()
     {
 
         this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, old_z);
 
         isDragging = false;
 
         if (this.transform.position.x == GameObject.Find("position_1").GetComponent<BoxCollider2D>().transform.position.x && this.transform.position.y == GameObject.Find("position_1").GetComponent<BoxCollider2D>().transform.position.y)
         {
             section1Full = true;
         }
 
     }
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.gameObject.name == "position_1")
         {
             isInSection1 = true;
             isInSection2 = false;
             isInSection3 = false;
             isInShelfArea = false;
             Debug.Log("Position 1");
 
         }
         else if (collision.gameObject.name == "position_2")
         {
             isInSection1 = false;
             isInSection2 = true;
             isInSection3 = false;
             isInShelfArea = false;
             Debug.Log("Position 2");
         }
         else if (collision.gameObject.name == "position_3")
         {
             isInSection1 = false;
             isInSection2 = false;
             isInSection3 = true;
             isInShelfArea = false;
             Debug.Log("Position 3");
         }
         else if (collision.gameObject.name == "shelf")
         {
             isInSection1 = false;
             isInSection2 = false;
             isInSection3 = false;
             isInShelfArea = true;
             Debug.Log("shelf");
         }
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (isDragging)
         {
 
             Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
             transform.Translate(mousePosition);
 
         }
         else
         {
 
             if (isInSection1)
             {
                 Debug.Log(section1Full);
                 if (section1Full == true)
                 {
                     transform.position = new Vector3(GameObject.Find(this.name + "_position").GetComponent<BoxCollider2D>().transform.position.x, GameObject.Find(this.name + "_position").GetComponent<BoxCollider2D>().transform.position.y, old_z ); ;
                     return;
                 }
                 else
                 {
                     transform.position = new Vector3(GameObject.Find("position_1").GetComponent<BoxCollider2D>().transform.position.x, GameObject.Find("position_1").GetComponent<BoxCollider2D>().transform.position.y, this.transform.position.z); ;
                     return;
                 }
 
             }
             else if (isInSection2)
             {
                 transform.position = new Vector3(GameObject.Find("position_2").GetComponent<BoxCollider2D>().transform.position.x, GameObject.Find("position_2").GetComponent<BoxCollider2D>().transform.position.y, this.transform.position.z);
 
             }
             else if (isInSection3)
             {
                 transform.position = new Vector3(GameObject.Find("position_3").GetComponent<BoxCollider2D>().transform.position.x, GameObject.Find("position_3").GetComponent<BoxCollider2D>().transform.position.y, this.transform.position.z);
 
             }
             else if (isInShelfArea)
             {
                 transform.position = new Vector3(GameObject.Find(this.name + "_position").GetComponent<BoxCollider2D>().transform.position.x, GameObject.Find(this.name + "_position").GetComponent<BoxCollider2D>().transform.position.y, this.transform.position.z);
 
             }
 
         }
 
     }
 }
 

dragdropissue.jpg (99.0 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

309 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

Related Questions

drag and merge object 0 Answers

Issues with collider or raycast 0 Answers

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

RectangleContainsScreenPoint works backwards. 0 Answers

2D drag script Need Help! 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