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 /
avatar image
0
Question by call_me_senpai · Mar 25 at 11:36 PM · drag-and-dropparent-child

how to change the parent of an object to another slot parent ?

i am making a rock paper scissors game but not the classic one that everyone knows, you have 12 cards, 3 blue cards of each type(3 paper cards,3 rock cards,3 scissors cards), 2 purple cards of each type and one last card that contains paper and scissors at the same time and for the game to start you have to arange those cards in slots. ![alt text][1] after a lot of thinking i made it work with drag&drop system but i faced some problems: - first problem that everytime i try to drop any card into the slot it returns to it's original position - second problem that i want the slot to become the parent of the card inserted in it but it just leave it's parent when i start dragging it then when i release it it returns to it's original parent(i even tried to delete some lines in my script to test if the second problem is caused by the first one or not,but it's the same even thought that the card get implemented in the slot and those codes aren't for nothing they return the card to it's original position if it's not implemented in a slot ) here the cards code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class DragDrop : MonoBehaviour, IPointerDownHandler,IBeginDragHandler,IEndDragHandler,IDragHandler
 {
     private RectTransform rectTransform;
     private CanvasGroup canvasGroup;
     public Text textField;
     [SerializeField] Canvas canvas;
     private Transform originalPos;
 
 
     private void Awake()
     {   
         rectTransform = GetComponent<RectTransform>();
         canvasGroup = GetComponent<CanvasGroup>();
     }
 
 
     public void OnBeginDrag(PointerEventData eventData)
     {
         canvasGroup.alpha = .5f;
         canvasGroup.blocksRaycasts = false;
         originalPos = transform.parent;
         transform.SetParent(transform.parent.parent);
     }
 
     public void OnDrag(PointerEventData eventData)
     {
         rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
     }
 
     public void OnEndDrag(PointerEventData eventData)
     {
         canvasGroup.alpha = 1f;
         canvasGroup.blocksRaycasts = true;
         transform.SetParent(originalPos);
         transform.localPosition = Vector3.zero;
         if (transform.localPosition == Vector3.zero)
         {
             textField.enabled = true;
         }
         
 
     }
 
     public void OnPointerDown(PointerEventData eventData)
     {
         
         
         textField.enabled = false;
         
         
     }
 }
     



and here the slots code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class slots : MonoBehaviour, IDropHandler
 
 {
     public GameObject slot;
     public void OnDrop(PointerEventData eventData)
     {
         if(eventData.pointerDrag != null) 
         {
             eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
         }
     }
 }
 


i really appreciate anything you can help me with cause i've been stuck in this for 2 days now

272080477-264271649172297-8859635335672306189-n.jpg (151.8 kB)
Comment
Add comment · Show 1
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
avatar image logicandchaos · Mar 26 at 04:09 PM 1
Share

in end drag you have transform.SetParent(originalPos); but you want to set the parent to the new position not the original.. and you don't get the transform for the new position anywhere in your code. You need to get the transform of the slot you want to be the new parent and then use SetParent with that transform. transform.SetParent(newPos);

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

136 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

Related Questions

Draggable in Scroll View Returning to proper place on Drop 0 Answers

SetParent does not work. 1 Answer

How to null children of main camera that is also a child? 1 Answer

Photon synchronization 1 Answer

Parenting a child to move on only one Axis ! 2 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