- Home /
Hey, i can't figure out, what my problem is. I want to Add something into the player's Inventory, but before that it should check, whether there is an Item with the same name in it, or not and if yes, if the maxAmount for that slot is reached.
I get this error: NullReferenceException: Object reference not set to an instance of an object
my Code:
private var parentInventory : InventoryButton;
private var inventoryScript : Inventory;
function Start()
{
parentInventory = this.transform.parent.GetComponent(InventoryButton);
inventoryScript = GameObject.Find("Player").GetComponentInChildren(Inventory);
}
function Nehmen()
{
for(var i = 0; i <= inventoryScript.mainInventory.Length(); i++)
{
if(inventoryScript.mainInventory[i].name == parentInventory.itemName &&
inventoryScript.mainInventory[i].amount < inventoryScript.mainInventory[i].maxStack)
{
if(inventoryScript.mainInventory[i].maxStack - inventoryScript.mainInventory[i].amount >=
parentInventory.itemAnzahlNehmen)
{
inventoryScript.mainInventory[i].amount += parentInventory.itemAnzahlNehmen;
parentInventory.parentInventory.tank[parentInventory.slot].amount -= parentInventory.itemAnzahlNehmen;
parentInventory.itemAnzahlNehmen = 0;
}
}
else if(inventoryScript.mainInventory[i].name == parentInventory.itemName &&
inventoryScript.mainInventory[i].amount >= inventoryScript.mainInventory[i].maxStack)
{
break;
}
else if(inventoryScript.mainInventory[i].name != parentInventory.itemName)
{
if(i == inventoryScript.mainInventory.Length() -1)
{
if(inventoryScript.mainInventory[i].name == "")
{
inventoryScript.mainInventory.Add(inventoryScript.items[parentInventory.itemID]);
Inventory_Add_Item.inventoryNewItemAdded = parentInventory.itemID;
parentInventory.parentInventory.tank[parentInventory.slot].amount -= parentInventory.itemAnzahlNehmen;
parentInventory.itemAnzahlNehmen = 0;
}
else
{
break;
}
}
}
}
}
Comment
Can you post your full NullReferenceException error, inc the line numbers etc?
Your answer
Follow this Question
Related Questions
how to detect wich item i have in my inevntory 0 Answers
How to combine items in inventory with words 0 Answers
[C#]Inventory script help. 3 Answers
Inventory AddItem help 1 Answer