Null reference List in chest/crate problem.

I have a list that is public on a crate/chest that you can loot.

I don’t know how to reference the empty spot in the list which stops the code from working properly if it isn’t full.

Code:

for(int cnt = 0; cnt < crateItems.Count; cnt++){


 //  draw the item texture.


     if(crateItems[cnt] == null){

    // Draw the empty texture.

}


}

Again: I can’t reference the empty slots, which also don’t let the rest of code work as in unless the public list crateItems is filled with gameobjects the code won’t show the other items in it.

Please assist thanks.

You said you’re using a list, right? I think you’ll have to switch to an array, that way you can initialize all the slots as null then start populating what you need with actual references to objects. Then everything else should work the same way.

Edit:
Actually, the empty slots would start off as null.

So it would look something like this

using UnityEngine;

public class Tasdf : MonoBehaviour
{
    public GameObject[] objs = new GameObject[100];

    private void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 100, 50), "check"))
        {
            for(int i = 0; i < objs.Length; ++i)
            {
                if(objs *!= null)*

{
Debug.Log(objs*.name);*
}
else
{
Debug.Log(“null”);
}
}
}
}
}
Drag this onto an empty GameObject on an empty scene and hit the button. You will see null in the console window. Drag some objects to the inspector list and you’ll see their names.