Hello reader,
Apologees in advance if this is a borderline retarded question to bring up. Been a while since I wrote anything, usually I find answers from other threads, but been banging my head on this one. I’m gonna try to be brief.
I’m trying to make an inventory system, with items, with a hotbar. For the items I use a scriptable object which I put a bunch of information into, as well as a GameObject. So basically on every scriptableobject I put a GameObject prefab into that field in the inspector.
On an OnDrop PointerEventData operation (that is when I in inventory view drop the item on a hotbar slot), I’d like to instantiate this gameobject to my scene, so that it exists in the world and I can toggle it with numkeys to activate the weapon. Also, this specific item that I need instantiated is only referred to through a method.
Below code is what I believe is relevant for the question, I cut out a lot from the full scripts to make it easier, I dare say nothing relevant is left out.
I would be grateful for any help or suggestions. I really am trying to find answers on my own before I ask, and usually it works out. But now I’m stuck for 2 days. Appreciate it.
BR
Tomas
Slot script:
public void AddItem(Item itemToAdd, int amnt)
{
if (itemToAdd == myItem)
{
myAmount += amnt;
}
else
{
myItem = itemToAdd;
myAmount = amnt;
}
ShowUI();
}
public void OnDrop(PointerEventData eventData)
{
// Adds item to the slot in the inventory view
AddItem(inventory.draggingItem, inventory.draggingAmount);
inventory.EndDrag();
// This I hope will instantiate an item for me, but it returns "Nullreferenceexception: Objects reference not set..."
if(this.gameObject.name.StartsWith("Hot"))
{
GameObject inHand = Instantiate(inventory.draggingItem.item3D, transform.position, Quaternion.identity, this.transform) as GameObject;
Debug.Log("Instantiated item");
}
}
Item class:
[CreateAssetMenu]
public class Item : ScriptableObject
{
//General
public string itemName;
public Sprite itemIcon;
//Stacking
public bool isStackable;
public int maxStackAmount;
// the GO field I populate in inspector
public GameObject item3D;
}
Inventory script:
public class Inventory : MonoBehaviour
{
public List<GameObject> slots = new List<GameObject>();
[HideInInspector]
public bool isDragging = false;
public Image draggingImage = null;
[HideInInspector]
public Item draggingItem;
[HideInInspector]
public int draggingAmount;
private GameObject draggingItem3D;
// the AddItem method referred to in slot script
public bool AddItem(Item itemToAdd, int amount)
{
Slot emptySlot = null;
for (int i = 0; i < slots.Count; i++)
{
Slot currentSlot = slots[i].GetComponent<Slot>();
if (currentSlot.myItem == itemToAdd && itemToAdd.isStackable && currentSlot.myAmount + amount <= itemToAdd.maxStackAmount)
{
currentSlot.AddItem(itemToAdd, amount);
return true;
}
else if (currentSlot.myItem == null && emptySlot == null)
{
emptySlot = currentSlot;
}
}
if (emptySlot != null)
{
emptySlot.AddItem(itemToAdd, amount);
return true;
}
else
{
print("Inventory is full!!!");
return false;
}
}
public void DoDrag(Item itemToDrag, int amnt)
{
draggingItem = itemToDrag;
isDragging = true;
draggingImage.enabled = true;
draggingImage.sprite = draggingItem.itemIcon;
draggingAmount = amnt;
draggingItem3D = draggingItem.item3D;
}
public void EndDrag()
{
draggingItem = null;
isDragging = false;
draggingImage.enabled = false;
draggingAmount = 0;
}
}