Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 frkbvt · Nov 29, 2021 at 05:31 PM · instantiatedrag-and-drop

Drag and Drop with instantiate

Hi,

I am a Beginner, and I try to do a Drag an Drop but from a gameObject which is instantiate many times.

Learning from a tutorial I have done this script right below, but if I have just one time the gameObject, it works perfectly, but when I had the instantiate script, I am not able to drag the gameObject after to be dropped the first time.

Thank you in advance for your answer !

script to DragandDrop :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class WaterDND : MonoBehaviour
 {
   private Vector3 screenPoint;
   private Vector3 offset;
   private Vector3 curPosition;
   private Vector3 otherPosition;
   private Vector3 curScreenPoint;
   private bool dropping = false;
 
     void Start()
     {
 
 Camera cam = gameObject.GetComponent();
     }
 
     void Update(){
       Debug.Log (curPosition + gameObject.name);
       screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
       curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
       curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
     }
 
   void OnMouseDown(){
 screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
   }
 
   void OnMouseDrag() {
   curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
   curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
   transform.position = curPosition;
   }
 
   void OnMouseUp() {
     if(dropping == false) {
     Destroy(gameObject);
         }
     else{
 transform.position = otherPosition;
     }
 
 
   }
 
   void OnTriggerEnter2D(Collider2D other){
 otherPosition = other.transform.position;
 
         if (other.tag == "slot")  {
             dropping = true;
 
                   if (other.name == "Slot AC"){
                     gameObject.transform.eulerAngles = Vector3.forward * 0;
                   }
                   if (other.name == "Slot AD"){
                     gameObject.transform.eulerAngles = Vector3.forward * -45;
                   }
                   if (other.name == "Slot CD"){
                     gameObject.transform.eulerAngles = Vector3.forward * -90;
                   }
                   if (other.name == "Slot ArD"){
                     gameObject.transform.eulerAngles = Vector3.forward * -135;
                   }
                   if (other.name == "Slot AG"){
                     gameObject.transform.eulerAngles = Vector3.forward * 45;
                   }
                   if (other.name == "Slot CG"){
                     gameObject.transform.eulerAngles = Vector3.forward * 90;
                   }
                   if (other.name == "Slot ArG"){
                     gameObject.transform.eulerAngles = Vector3.forward * 135;
                   }
                   if (other.name == "Slot ArC"){
                     gameObject.transform.eulerAngles = Vector3.forward * 180;
                   }
                   if (other.name == "Slot CC"){
                     gameObject.transform.eulerAngles = Vector3.forward * 0;
                   }
 
         }
   }
 
   void OnTriggerExit2D(Collider2D other){
             dropping = false;
             }
 }



Script to Instantiate

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 using UnityEngine.EventSystems;
 
 public class generateGeode : MonoBehaviour
 {
 
 public GameObject myPrefab;
 private bool done = true;
 
 
 void OnMouseDrag(){
 
     if (done == true){
 done = false;
     Instantiate(myPrefab, new Vector3(640,30,0), Quaternion.identity, GameObject.FindGameObjectWithTag("Canvas").transform);
 myPrefab.transform.localScale = new Vector3 (15, 15, 0);
 
     }
 
   }
 }


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

195 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

Related Questions

change color of instantiated object 1 Answer

Unity [PUN] New Instantiated players cannot see previously instantiated players 0 Answers

Why my object dont want to Update ? 0 Answers

Prefab not Instantiating 0 Answers

Instantiating GameObject loses some properties of the original GameObject 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