- Home /
Inventory system help - getMouseButtomDown problem
this is my code for items:
using UnityEngine; using System.Collections;
public class Item : MonoBehaviour {
public Transform Inventoy;
public InventoryContoller IC;
void OnMouseEnter()
{
Inventoy.GetComponent<InventoryContoller>().selectedItem = this.transform;
}
}
and this is the inventory controller using UnityEngine; using System.Collections;
public class InventoryContoller : MonoBehaviour {
public Transform selectedItem, selectedSlot, originalSlot;
public bool canGrab = false;
void Update () {
if(Input.GetMouseButton(0) && selectedItem != null)
{
selectedItem.position = Input.mousePosition;
}
else if(Input.GetMouseButtonUp(0) && selectedItem != null)
{
selectedItem.localPosition = Vector3.zero;
}
}
}
i dont know why, but thats dont work... can someone help me? the inventory controller dont even get the transform from the item script
Answer by Ma_ti · Jul 19, 2016 at 03:30 AM
Try if that works
using UnityEngine.EventSystems;
public class Item : MonoBehaviour, IPointerEnterHandler
{
public Transform Inventoy;
public InventoryContoller IC;
public void OnPointerEnter(PointerEventData eventData)
{
Inventoy.GetComponent<InventoryContoller>().selectedItem = this.transform;
}
}
Is there error or just nothing happens? Also on what kind of object is Item script, image?
there is anyway to disble the RaycastTarget from script? because if i hold the item, i cant get the Slot :(
in you inventory controller ...
if(Input.Get$$anonymous$$ouseButton(0) && selectedItem != null)
{
selectedItem.position = Input.mousePosition;
selectedItem.GetComponent<Image>().raycastTarget = false;
}
(check this for spelling mistakes)
Answer by wojtask12 · Jul 19, 2016 at 12:17 AM
OnMouseEnter method works only for object with colliders/GUI elements - make sure you have it attached to your object. You may also find some help here: http://docs.unity3d.com/Manual/SupportedEvents.html