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 /
avatar image
0
Question by Froschkopf · Jun 06, 2015 at 04:45 PM · dragdrag-and-drop

Need help debugging: SiblingIndex

Hi there!

Trying to create a card game. Player should be able to rearrange the cards in their hand. To show where the card the player is currently dragging was before I create a placeholder. The placeholder is destroyed at the end of the drag. Everything runs pretty smooth, but when I rearrange the cards for a while it gets a little chaotic: at the end I always have 1 card that is at a different SiblingIndex as it should be. Would be lovely if you could take a look please.

Here's the code: (every GameObject Card got a Script(c#) called Draggable)

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
 {
     
     public Transform parentToReturnTo = null;                    
     public Transform placeholderParent = null;
     public GameObject placeholder = null;
 
     //public GameObject preFab;
 
     
     public void OnBeginDrag (PointerEventData eventData)
     {
         
         placeholder = new GameObject ();
         //placeholder = Instantiate (preFab) as GameObject;
         parentToReturnTo = this.transform.parent;                        
         placeholder.transform.SetParent (parentToReturnTo);
         placeholder.transform.SetSiblingIndex (this.transform.GetSiblingIndex ());    
         placeholder.transform.localPosition = this.transform.localPosition;
         placeholder.transform.localRotation = this.transform.localRotation;
 
         this.transform.SetParent (this.transform.parent.parent);
         this.GetComponent<CanvasGroup> ().blocksRaycasts = false;
 
     }
     
     
     
     
     
     public void OnDrag (PointerEventData eventData)
     {
         this.transform.position = eventData.position;
         
         placeholderParent = placeholder.transform.parent;
         int tempSI = placeholderParent.childCount-1;
         for (int i=0; i < placeholderParent.childCount; i++) {
             if (this.transform.position.x < placeholderParent.GetChild (i).position.x) {
                 tempSI = i;
                 if (placeholder.transform.GetSiblingIndex () < tempSI)
                     tempSI--;
                 break;
             }
         }
 
         Vector3 tempPos = placeholderParent.GetChild (tempSI).transform.localPosition;
         Vector3 tempPosPlace = placeholder.transform.localPosition;
         Quaternion tempRot = placeholderParent.GetChild (tempSI).transform.localRotation;
         Quaternion tempRotPlace = placeholder.transform.localRotation;
 
         placeholderParent.GetChild (tempSI).transform.localPosition = tempPosPlace;
         placeholderParent.GetChild (tempSI).transform.localRotation = tempRotPlace;
         
         placeholder.transform.localPosition = tempPos;
         placeholder.transform.localRotation = tempRot;
 
         this.transform.localRotation = tempRot;
 
         placeholder.transform.SetSiblingIndex (tempSI);
     }
     
     
     
     
     
     public void OnEndDrag (PointerEventData eventData)
     {
         this.transform.SetParent (parentToReturnTo);
         this.transform.SetSiblingIndex (placeholder.transform.GetSiblingIndex ());
         Vector3 temp = placeholder.transform.localPosition;
         GetComponent<CanvasGroup> ().blocksRaycasts = true;
         Destroy (placeholder);
         this.transform.localPosition = temp;
     }
     
 }


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 AlwaysSunny · Jun 06, 2015 at 04:45 PM 0
Share

Format pasted code with 101010 button. This has been done for you just this once; improper formatting is grounds for rejection.

Honestly, I wouldn't expect help proofreading a snippet this large / complex. Seems to me you could bypass this issue by letting index enforcement be a separate procedure that is based on the transform's position. Wherever possible, I avoid futzing with sibling order. I realize it's critical in UI stuff, but even then there's usually a way to take care of it that's simpler than trying to manage it directly alongside your manipulation logic.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Drag and Drop on RTS camera 1 Answer

Drag Item Scaling on Phone 0 Answers

How to move UI image between panels 0 Answers

How to find in what Scripts or components an Object is connected 0 Answers

Line Direction 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