reset a GameObject parents childs to 0 if a item has changed its parent
I'm working Right now on a game where someone else has programmed an inventory System with this tutorial: Klick
My Problem Right now is, if I drag an item into another Slot of the inventory, it's getting set to the other Slot as a child but the old Slot is also having still a child so the childcount is bigger than 0.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Slot : MonoBehaviour, IDropHandler
{
public GameObject item
{
get
{
if (DragHandeler.oldParent.GetChild(0).gameObject != DragHandeler.itemBeingDragged)
{
Debug.Log("2 tf: " + transform.childCount);
return transform.GetChild(0).gameObject;
}
else if (transform.childCount > 0)
{
Debug.Log("1 tf: " + transform.childCount);
return transform.GetChild(0).gameObject;
}
Debug.Log("get item else");
return null;
}
}
#region IDropHandler implementation
public void OnDrop(PointerEventData eventData)
{
if (!item)
{
DragHandeler.itemBeingDragged.transform.SetParent(transform);
//ExecuteEvents.ExecuteHierarchy<IHasChanged>(gameObject, null, (x, y) => x.HasChanged());
Debug.Log("Innerhalb: " + transform.childCount);
}
Debug.Log("Außerhalb: " + transform.childCount);
}
#endregion
}
I hope someone can help me with this one. I'm working in Unity 2017.2
Answer by GottesSchaf · Nov 06, 2018 at 12:03 PM
Got the solution: I needed to add a Panel as parent from the Slots. Somehow it works then.
Answer by Anarevian · Nov 05, 2018 at 10:52 PM
Have you tried with deleting the child object as soon as it gets dragged out of the slot and get put to antoher Slot? because then you would have a child count of 0 after its dropped onto antoher slot.
Doesn't work. If i destroy the GameObject (child) it will be destroyed even if it's on the new parent. I even tried to use transform.DetachChildren(); but this doesn't work either.