so something always goes wrong with my code … and then i get stuck debugging for a month or two. hoping to avoid that this time.
i now have an item asset. i am trying to get the hero to be able to pick up an item and i am having a problem with my Item class functions - the console says there is an “Object reference not set to an instance of an object” error in the Inv_Add and Pick_Up functions:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace CoS
{
//Wrapper class containing old info in a prettier way
public class Item : ScriptableObject
{
heroInventory inv;
public Transform hero;
GameObject itemonground;
private void Start()
{
inv = new heroInventory();
}
//do i need swinging and thrusting?
//Make all of these readonly so you can be sure they won't be changed.
public string objname;
public string description;
public double price;
public int quantity; //this doesn't belong here?
public string sprite;
public string type; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public int dr_crushing; //need to make objects for these, so i don't have so many properties
public int dr_cutting;
public int dr_impaling;
public int pd_crushing;
public int pd_cutting;
public int pd_impaling;
public int thrusting_modifier;
public int swinging_modifier;
public Item(string objname, string description, double price, int quantity, string sprite, string type,
int dr_crushing, int dr_cutting, int dr_impaling, int pd_crushing, int pd_cutting, int pd_impaling,
int thrusting_modifier, int swinging_modifier)
{
this.objname = objname;
this.description = description;
this.price = price;
this.quantity = quantity;
this.sprite = sprite;
this.type = type;
this.dr_crushing = dr_crushing;
this.dr_cutting = dr_cutting;
this.dr_impaling = dr_impaling;
this.pd_crushing = pd_crushing;
this.pd_cutting = pd_cutting;
this.pd_impaling = pd_impaling;
this.thrusting_modifier = thrusting_modifier;
this.swinging_modifier = swinging_modifier;
}
/**********************
* Inventory Functions
* *******************/
// remove from inventory
public void Inv_Remove(Item item, int qnty)
{
//DID THIS WRONG! Need inventory list
//In the script we will remove an item from the inventory
var found = -1;
//We will check if the item exists and take its place
for (var i = 0; i < 5; i++)
{
//ItemID eyedee1 = (ItemID)i;
if (inv.inventory[i].objname == item.objname) //maybe have a unique id rather than use name? possibility.
{
found = i;
break;
}
}
if (found != -1)
{
//If we found the item, we remove it
inv.inventory[found].quantity -= qnty;//qnty is the amount to remove
if (inv.inventory[found].quantity <= 0)
{
inv.inventory[found] = ItemDatabase.items[ItemDatabase.ItemID.NOTHING];
} //If there is no more of the item we change the place to an empty place (Nothing item)
}
}
//In this script we will add items to the inventory
public void Inv_Add(Item item, int qnty)
{
var found = -1;
bool fullflag = false;
for (var i = 0; i < inv.invcapacity; i++) // !!!!!!This is the line the debugger is pointing out
{
if (inv.inventory[i].objname == item.objname)
{
found = i; //We affect to found the place of the item if we find it
break;
}
}
//If it doesn't exist
if (found == -1)
{
//Let's check if there is an empty place to add our item
for (var j = 0; j < inv.invcapacity; j++)
{
if (inv.inventory[j].objname == "Nothing")
{
found = j;
break;
}
else if (j == (inv.invcapacity - 1) && found != j)
{
fullflag = true;
break;
}
}
}
//should have an "inventory is full" contingency
if (fullflag)
{
// print this out! in a game-friendly gui - depends on whether looting or picking up, or buying
Debug.Log("inventory is full, cannot pick up object");
}
//Now let's add our item to the place we have *
if (found != -1)
{
inv.inventory[found] = item; //Argument 0 is the id of the item
inv.inventory[found].quantity += qnty; //Argument 1 is the amount of the item to add
}
// need a script for looting, for buying, and for picking up
}
// Drop an item
public void Inv_Drop(Item item)
{
// item = item clicked in gui - right click and click drop
//Instantiate(hero, item);
//not sure what to use in place of "item"
//use groundobject prefab
Inv_Remove(item, 1);
}
//Pick up item
public void Pick_Up(Item item)
{
Inv_Add(item, 1);
}
}
}