How to render an image always on top? Already tried some solutions
Hi,
I'm making a small crafting game and I first did the drag and drop mechanic using the Eventsystem Interfaces DragHandler and DragDrop (I say this in case you need this info to help me).
So everything works fine except that when the item is being dragged it goes behind the the other images and when I drop it it goes back to being on top, just like the other items on the image.
Summarizing the drag process: when the item is being dragged it is placed as child of a DragParent object and when placed on a slot or the inventory it changes its parent to the one it is using (Ingredient 1 or 2 or item pool.
Here is what I have tried:
*Setting the items to the UI Layer, as well as the Dragparent and everything else on the Default layer. The items still render on the back when dragged.
*Moving the drag parent on the Z acces closer to the camera. The items still render on the back when dragged.
*Moving the DragParent to the bottom on the hierachy so that it renders on top of everything. It did work, but for some reason I don't understand that made my DropSlot unable to pickup the item. So its not a viable solution.
So how do I make teh items always render on top despite the hierachy? Or why my dropSlot stops working? How do I make them work again? I' dont know a lot about the eventSystem.
DropSlot code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DropSlot : MonoBehaviour, IDropHandler
{
public GameObject item;
// Start is called before the first frame update
void Start()
{
}
public void OnDrop(PointerEventData eventData)
{
if(item == null)
{
item = DragHandler.itemDragging;
item.transform.SetParent(transform);
item.transform.position = transform.position;
}
//throw new System.NotImplementedException();
}
// Update is called once per frame
void Update()
{
if (item != null && item.transform.parent != transform.parent)
{
item = null;
}
}
}
Your answer
Follow this Question
Related Questions
OnPointerEnter blocked by something 2 Answers
EventSystem raycasting on World Canvas gameobject always returning worldPosition of zero 2 Answers
If I disable/enable my canvas (pause game menu), controller navigation in the canvas stops to work? 1 Answer
UI Mask for List VS Canvas for Text 0 Answers
EventSystem.current is returning null 0 Answers