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