• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by kristabice · Mar 18, 2016 at 12:42 PM · drag-and-drop

Incrament problem

Hi! So I am super new to C# and I have been working on the same problem for a few days now and I cant seem to get it to work. So I just wanted to get some help!! I am trying to write a program where when an object is dropped into a certain place it increments by the price float that is set up. However, since part of the requirement for this project is that the objects 'snap' into place so I have a grid layout and then canvas on that layout for aesthetics. The problem I have is that I can get it to add but only if i drop the objects into the same compartment every time. When I take away the compartments I seem to have trouble with my drag and drop script working properly. I could really use the help!! sorry, this is probably a dumb question but I really cannot figure it out. I am brand new to coding and at wits end with this problem!!

This is my drop script: public Text totaltext; public int maxCount; public float donutPrice = 1.99f; public float donutPriceTwo = 1.99f; public float total; public float newTotal;

     public  GameObject item {
     get {
         if(transform.childCount>0){
             return transform.GetChild (0).gameObject;
         }
         return null;
     }


 }
 
 
 public void OnDrop (PointerEventData eventData)
 {

     if (!item) {
         DragHanddler.itemBeingDragged.transform.SetParent (transform);
         if (gameObject.tag == "Slot" || gameObject.tag == "Background" || gameObject.tag == "DonutBox") {
             Destroy (DragHanddler.itemBeingDragged);
             //Debug.Log(DragHanddler.itemBeingDragged +"has been destroyed");
         }
         
     }

     if (gameObject.tag == "Inventory") {
         if (DragHanddler.itemBeingDragged == true)
               
             total = total + donutPrice;
         totaltext.text = "Total: " + total.ToString ();

         Debug.Log (total);
     }
 
     
     
 }

 }

and this is my script for dragging:

using UnityEngine; using System.Collections; using UnityEngine.EventSystems;

public class DragHanddler : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler { public static GameObject itemBeingDragged;

 Vector3 startPosition;
 Transform startParent;



 public void OnDrag (PointerEventData eventData)
 {
     transform.position = Input.mousePosition;

 }



 public void OnBeginDrag (PointerEventData eventData)
 {

     itemBeingDragged = gameObject;
     startPosition = transform.position;
     startParent = transform.parent;

     PrefabSrcipt.Instance.Donut = itemBeingDragged;
     GameObject Clone = Instantiate(PrefabSrcipt.Instance.Donut);
     Clone.transform.parent = startParent;
     GetComponent<CanvasGroup> ().blocksRaycasts = false;
     Clone.name = itemBeingDragged.name;

 }



 public void OnEndDrag (PointerEventData eventData)
 {    
     itemBeingDragged = null;
     GetComponent<CanvasGroup> ().blocksRaycasts = true;

     
     
     if (transform.parent == startParent) {
         transform.position = startPosition;
     }

          
 
 }





}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Drag and drop 3d game 1 Answer

Dragging an object in iOS (UnityScript) 3 Answers

how can i put just one child in a drag and drop container in ngui 1 Answer

How to create Arrows Between Two objects and Animate them 2 Answers

How do I detect OnDrop event on a GameObject? 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