Problem with using an item from inventory
Hi,
so here's the deal:
I followed a tutorial for an inventory. In this tutorial you use the item when clicking on it. I on the other hand would like a submenu to pop up when clicking on an item where you can choose weather to "use", "combine" or "cancel".
So here is the code from the tutorial which works fine.
public class Slot : MonoBehaviour, IPointerClickHandler {
private Stack<Item> items;
public Sprite slotEmpty;
public Sprite slotHighlighted;
public bool IsEmpty{
get{return items.Count == 0;}
}
private GameObject useButtons;
// Use this for initialization
void Start () {
items = new Stack<Item> ();
RectTransform slotRect = GetComponent<RectTransform>();
useButtons = GameObject.Find("Canvas/Use Buttons");
useButtons.active = false;
}
// Update is called once per frame
void Update () {
}
public void AddItem (Item item){
items.Push (item);
ChangeSprite (item.spriteNeutral, item.spriteHighlighted);
}
private void ChangeSprite(Sprite neutral, Sprite highlight)
{
GetComponent<Image> ().sprite = neutral;
SpriteState st = new SpriteState ();
st.highlightedSprite = highlight;
st.pressedSprite = neutral;
GetComponent<Button> ().spriteState = st;
}
private void UseItem()
{
if (!IsEmpty) {
items.Pop ().Use ();
useButtons.active = false;
if (IsEmpty) {
ChangeSprite (slotEmpty, slotHighlighted);
Inventory.EmptySlots++;
}
}
}
/*private void Benutzen()
{
useButtons.active = true;
}
public void UseButon()
{
UseItem ();
}*/
#region IPointerClickHandler implementation
public void OnPointerClick (PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Left)
{
UseItem();
}
}
#endregion
}
My code is in the / / thingis. When I activate these lines and chance the "OnPointerClick" command to "Benutzen();" it gives me a NullReferenceException on line 16.
Does anyone know what to do?
Thanks in advance ;)
Sure :)
NullReferenceException: Object reference not set to an instance of an object Slot.get_IsEmpty () (at Assets/Standard Assets/InvScripts/Slot.cs:16) Slot.UseItem () (at Assets/Standard Assets/InvScripts/Slot.cs:53) Slot.UseButon () (at Assets/Standard Assets/InvScripts/Slot.cs:71) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:149) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:626) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:766) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:54) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData,
Looks like items is null, which is weird because it was initialized in Start... not sure what's happening here. $$anonymous$$ake sure Start is indeed called. Try initializing items at declaration like so private Stack<Item> items = new Stack<Item>();
and remove items = new Stack<Item> ();
from Start.
Your answer
Follow this Question
Related Questions
switch between 5 Ui icons by pressing a button 0 Answers
Display list as UI or GUI 0 Answers
Inventory and item class design 0 Answers
Why is drag and drop not working? 1 Answer
Start and Stop Timer Help 1 Answer