Hi all,
I’m new to coding (I’m sure you’ve read that a few hundred times).
I’ve been working though a tutorial series on a popular video upload site.
and am up to the point where items in bag can now be deleted by clicking the x in the icons top right corner.
the tutorial does not cover adding a gui box which asks the user to confirm they want the item gone forever, I’ve tried almost 15 different videos, none of which cover what I want but I tried to make alterations to the code to achieve my goal, non worked, all it did was throw errors in different ways dependant on what I did.
I’ve created a parent object, this has the text i need and the child buttons for confirm or cancel, I just can’t seem to build it into the existing code.
using UnityEngine;
using UnityEngine.UI;
public class InventorySlot : MonoBehaviour
{
public Image icon;
public Button removeButton;
Item item;
public void AddItem (Item newItem)
{
item = newItem;
icon.sprite = item.icon;
icon.enabled = true;
removeButton.interactable = true;
}
public void ClearSlot ()
{
item = null;
icon.sprite = null;
icon.enabled = false;
removeButton.interactable = false;
}
public void OnRemoveButton ()
{
Inventory.instance.Remove(item);
}
}
the OnRemoveButton is where I’d like the gui confirmation to trigger, before the item is removed, probably with an if statement? no matter what I do I can’t get it working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
#region Singleton
public static Inventory instance;
private void Awake()
{
if (instance != null)
{
Debug.LogWarning("More than one instance of Inventory found!");
}
instance = this;
}
#endregion
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback;
//amount of bag slot
public int space = 20;
public List<Item> items = new List<Item>();
public bool Add (Item item)
{
if (!item.isDefaultItem)
{
if (items.Count >= space)
{
Debug.Log("Not enough room in Bag");
return false;
}
items.Add(item);
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
return true;
}
public void Remove (Item item)
{
items.Remove(item);
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
}
It’s a challenge for myself really but I just can’t get it working, please could someone help?
thank you