- Home /
Why are objects just set to null?
Hi, I'm currently writing an inventory system for my game in unity and i have a little problem concerning the array in which i save my items. To store my items in the inventory i unite items of the same type into an item stack and then i store the item stack into an one dimensional array. The ItemStack class is a standard class which derives from nothing and my Inventory is a MonoBehaviour class and contains the array. Here is the simplified code: The ItemStack class basically contains the item which is stored in the stack and the amount.
public class ItemStack
{
private Item stackItem;
private int stackSize;
public ItemStack(Item item, int amount)
{
stackItem = item;
stackSize = amount;
}
public ItemStack()
{
stackItem = null;
stackSize = -1;
}
}
The Inventory class holds the array:
public class Inventory : MonoBehaviour
{
public ItemStack[] inventory = new ItemStack[20];
}
To add a stack to the inventory i do following:
public bool StoreInFirstFreeSlot(Item item)
{
for (int i = 0; i < inventorySize; i++)
{
if (inventory[i] == null)
{
ItemStack stack = new ItemStack(item, 1);
inventory[i] = stack;
return true;
}
}
return false;
}
Until here everything is fine. I initialize a new ItemStack and give it the item and the amount. Right after that line the item in the item stack is not null. But when i want to access the Stack later on it says that the item in the stack is null. But i can still access the methods and values of the item. Why is the item null when i want to access it later ? I know that an object can be fake null but why is the variable stackItem even deleted or set to null in the itemStack class ?
Are you using inheritance in this context ? You could also add the [System.Serializable] tag to the ItemStack to monitor in the inspector what's going on ? (will make the content of the public array in the monobehavior visible)
Yeah i already tried to figure it out with using [System.Serializable] but all it says is that the item variable is missing. Another weird thing is that when i initialize and add the itemStack in a completely different class the item variable is not being set to null. I just dont understand it. It makes absolutely no sense to me. I do the exact same thing in the Inventory class itself and the item variable is null
But when i want to access the Stack later on it says that the item in the stack is null. But i can still access the methods and values of the item.
I don't believe this is possible, so I would advise having another look and modifying your description of what the problem is. And show us the access code that's finding a null item because on the face of it, that would appear to be where things are going wrong.
Answer by Junkawitsch · Dec 19, 2018 at 06:13 PM
public class Inventory : MonoBehaviour
{
public static Inventory instance;
public int inventorySize = 20;
public ItemStack[] inventory = new ItemStack[20];
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback;
void Awake()
{
if (instance != null) {
Debug.Log ("More than one inventory found");
return;
}
instance = this;
}
void Start()
{
//inventory = new ItemStack[inventorySize];
}
public bool Add (Item item)
{
List<ItemStack> itemStacks = FindStacksWith(item);
for(int i = 0; i < itemStacks.Count; i++)
{
if(itemStacks[i].HasFreeSpace())
{
itemStacks[i].Push(item);
return true;
}
}
bool returnBool = StoreInFirstFreeSlot(item);
InventoryChanged();
return returnBool;
}
public bool Add(ItemStack stack, int i)
{
if (inventory[i] != null) return false;
inventory[i] = stack;
InventoryChanged();
return true;
}
public void InventoryChanged()
{
if (onItemChangedCallback != null)
{
onItemChangedCallback.Invoke();
}
}
public ItemStack FindStackWith(Item item)
{
foreach (ItemStack stack in inventory)
{
if(stack != null && stack.Peek() == item)
{
return stack;
}
}
return null;
}
public List<ItemStack> FindStacksWith(Item item)
{
List<ItemStack> itemStacks = new List<ItemStack>();
for(int i = 0; i < inventory.Length; i++)
{
if (inventory[i] != null && inventory[i].Peek().itemID == item.itemID)
{
itemStacks.Add(inventory[i]);
}
}
return itemStacks;
}
public bool StoreInFirstFreeSlot(Item item)
{
for (int i = 0; i < inventorySize; i++)
{
if (inventory[i] == null)
{
ItemStack stack = new ItemStack(item, 1);
Add(stack, i);
Debug.Log("S.I.F.F.S" + item);
return true;
}
}
return false;
}
}
That's the Inventory class
public class ItemStack
{
private Item stackItem;
private int stackSize;
public ItemStack(Item item, int amount)
{
stackItem = item;
stackSize = amount;
}
public ItemStack()
{
stackItem = null;
stackSize = -1;
}
public Item Peek()
{
return stackItem;
}
public bool Push(Item item)
{
Debug.Log("" + item.itemID);
if(stackItem == null)
{
stackItem = item;
stackSize = 1;
return true;
}
if (item.itemID != stackItem.itemID || stackSize >= GetMaxStackSize()) return false;
stackSize++;
return true;
}
public int GetStackSize()
{
return stackSize;
}
public int GetMaxStackSize()
{
if(stackItem == null)
{
return -1;
}
else
{
return stackItem.itemMaxStackSize;
}
}
public bool HasFreeSpace()
{
if (stackSize < GetMaxStackSize()) return true;
return false;
}
}
That's the ItemStack
so now if a call the Add(Item item) method in the inventory class the stack is being added to the array but the variable stackItem which is assigned in the constructor ItemStack(Item item, int amount) gets null
Answer by Matt1000 · Dec 19, 2018 at 02:30 PM
Wait a second, so... Your array is of ItemStack objects. And you actually create it and add it to your inventory. Although that doesn't mean that that stack's item variable has a not null object. You may be creating a ItemStack with a null object.
If I were you I'd add a
if (item == null) {
Debug.Log("Null Item");
return false;
}
inside your method and check what happens.
I already tested if the item i add to the stack is null. It isnt.
If i check if the item variable in the stack is null directly after i add the stack to the inventory array it says that the item is not null. But anywhere else the item is null.
Prove it! To yourself as much as to us.
Clearly, either you're accessing a different stack, accessing an element in the stack's array that you haven't populated yet, or you've removed the element in between. So show us the accessing code, and explain why you expect the element in the stack's array to be non-null at that time.
$$anonymous$$y full code is awaiting ad$$anonymous$$istration. The weird thing is that the statement "stackItem == null" is returning true but i can do stackItem.method() in the following line and i do not get any error. For example: I can get the name of the "null-item" with stackItem.itemName and it returns the correct name, but the stackItem is still null. That means that the item was correctly added to the stack but somehow got set to null. I think that the object is fake null. link text
Your answer
Follow this Question
Related Questions
How to access variable from other class? 1 Answer
Distribute terrain in zones 3 Answers
Why is my C# array of objects fully populated when some of them should be null? 3 Answers
Can't get script to access other script/class (c#) 1 Answer
How can I access a function without knowing the script/class name to which it belongs? 1 Answer