Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Grid Layout Group, Stacking object

Discussion in 'Scripting' started by Josenifftodd, Feb 12, 2016.

  1. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Is they any possible way to make objects stay on top of each other in a list? as I have my cards spawn in one spot (spawnpoint) and when I drag my cards away they don't spring back to the spawn point.

    My cards which are in the Grid Layout Group, bounce straight back as intended.

    I'm using this script to drag my objects around on the scene.


    Code (CSharp):
    1. public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    2. {
    3.  
    4.     Transform parentToReturnTo = null;
    5.  
    6.     public void OnBeginDrag(PointerEventData eventData)
    7.     {
    8.         Debug.Log("OnBeginDrag");
    9.  
    10.         parentToReturnTo = this.transform.parent;
    11.         this.transform.SetParent(this.transform.parent.parent);
    12.     }
    13.  
    14.     public void OnDrag(PointerEventData eventData)
    15.     {
    16.         Debug.Log("OnDrag");
    17.         this.transform.position = eventData.position;
    18.     }
    19.  
    20.     public void OnEndDrag(PointerEventData eventData)
    21.     {
    22.         Debug.Log("OnEndDrag");
    23.         this.transform.SetParent(parentToReturnTo);
    24.     }
    25.  
    26.  
    27.  
    28. }
    bare in mind that it does go back to the spawnSpot's child once i let go, it just doesn't bounce back to the location. I can attach a screenshot if needed.
     
  2. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Figured it out, when using Grid Layout Group. Make sure to go into the -120 for spacing, and then you can line them above each other if you're instantiating from a list.
     
    MiddlemanMorgs likes this.