- Home /
Check if an array index is empty
I'm trying to find the first empty spot in my array, then add something there. The problem is this code:
if (inventory[i] == null)
{
inventory[i] = item;
print("Added item");
return;
}
Without the check, it will add an item to the array, but only in the 1st spot of 6, no matter what's there. Removing the return makes it use all 6 slots of course.
With the null check, it doesn't meet the condition. I think it's because of the class I'm using?
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour {
public InventoryItem[] inventory;
[System.Serializable]
public class InventoryItem
{
public GameObject worldObject;
public Texture2D texRepresentation;
}
void Awake()
{
inventory = new InventoryItem[6];
}
public void AddItem(InventoryItem item)
{
for(int i = 0; i < inventory.Length; i++)
{
if (inventory[i] == null)
{
inventory[i] = item;
print("Added item");
return;
}
}
print("Inventory full");
}
public void NewItem(GameObject worldObject, Texture2D texRep)
{
var newItem = new InventoryItem();
newItem.worldObject = worldObject;
newItem.texRepresentation = texRep;
AddItem(newItem);
}
}
What do you mean by doesn't meet the condition? Do you mean that the '== null' test never evaluates to true, even when there are empty slots in the inventory?
I don't see any reason why AddItem wouldn't work as-is. The problem is likely where you're removing items from the list in some other code.
Yes, I mean that it never evaluates to true even when the entire array is empty. I think it's because each Element of the array holds a GameObject/Texture2D, so I need to check if both those slots are null, but I'm not an advanced programmer.
Answer by Stardog · May 27, 2011 at 12:40 PM
Solved it. It was much simpler than I thought!
if (inventory[i].worldObject == null && inventory[i].texRepresentation == null)
{
inventory[i] = item;
print("Added item");
return;
}
Full script:
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour {
public InventoryItem[] inventory;
public bool displayInv = false;
[System.Serializable]
public class InventoryItem
{
public GameObject worldObject;
public Texture2D texRepresentation;
}
void Awake()
{
inventory = new InventoryItem[6];
}
void Update()
{
if (Input.GetButtonDown("Inventory Key"))
{
if (!displayInv)
{
print("Toggle Inv ON");
displayInv = true;
}
else
{
print("Toggle Inv OFF");
displayInv = false;
}
}
}
public void AddItem(InventoryItem item)
{
for(int i = 0; i < inventory.Length; i++)
{
if (inventory[i].worldObject == null || inventory[i].texRepresentation == null)
{
inventory[i] = item;
print("Added item");
return;
}
}
print("Inventory full");
}
public void NewItem(GameObject worldObject, Texture2D texRep)
{
var newItem = new InventoryItem();
newItem.worldObject = worldObject;
newItem.texRepresentation = texRep;
AddItem(newItem);
}
void OnGUI()
{
if (displayInv)
{
GUI.Box(new Rect(Screen.width / 2, Screen.height / 2,250,250), "test");
}
}
}