Hi all. I’m having some trouble with this script for a loot container. I’ve attached the whole thing below because it won’t make sense otherwise.
The idea is that this is attached to every loot container in game. Each container uses the same UI object to display the loot inside. I’m using an Item[ ] on each container to actually store the items and use ContainerSlot[ ] to handle the UI updating of each slot (grabs the item from same index in Item[ ] and changes the image on that index in ContainerSlot[ ]).
In SelectSlot(ContainerSlot s) I use an int variable to get the index of s in the ContainerSlot[ ] and store it to later refer to the same index in the Item[ ]. (This function gets called by a button on whichever slot is clicked, passing that slot as a parameter).
Here’s my problem. When we get to the two functions at the bottom (UpdateButtons() and ContainsItems() neither of these work as expected. In both functions I’m trying to check if a certain index of the Item[ ] does not equal null. I’ve serialized this array so I can actually see what is at each index of it. As expected the AddLoot(List items) function works as intended and adds items to the array from the loot table passed in when opening the container. However even if I’m checking an index which I can see from the inspector does contain an Item my checks on this index in the bottom 2 functions are not picking this up.
Considering I do this exact same null check in the OnOpen() function to set the sprite for each slot and it works fine there I can’t figure out why it would not work the same way in the other functions. Been staring at this for over an hour now and would really appreciate some advice. Thanks in advance!
public class Container : MonoBehaviour
{
[SerializeField] private GameObject _containerUI;
public GameObject containerUI { get { return _containerUI; } }
private ContainerSlot[] _slots;
private ContainerButtons[] _buttons;
[SerializeField] private Item[] _items;
private ContainerSlot _selected;
private bool _looted = false;
[SerializeField] private int _selectedIndex = 0;
public ContainerSlot selected { get { return _selected; } set { _selected = value; } }
public List<Item> loot = new List<Item>();
void Start()
{
_slots = _containerUI.GetComponentsInChildren<ContainerSlot>();
_items = new Item[_slots.Length];
_buttons = _containerUI.GetComponentsInChildren<ContainerButtons>();
foreach (ContainerSlot slot in _slots)
{
slot.container = this;
}
foreach (ContainerButtons button in _buttons)
{
button.container = this;
}
}
public void ToggleUI()
{
_containerUI.SetActive(!_containerUI.activeSelf);
OnOpen();
}
public void AddLoot(List<Item> items)
{
int rand = Random.Range(0, 2);
int quantity = 0;
if(rand == 0)
{
quantity = 1;
}
else if(rand == 1)
{
rand = Random.Range(1, 3);
if(rand == 1)
{
quantity = 2;
}
else if(rand == 2)
{
rand = Random.Range(2, 4);
if(rand == 2)
{
quantity = 3;
}
else if(rand == 3)
{
quantity = 4;
}
}
}
for(int i = 0; i < quantity; i++)
{
_items[i] = items[Random.Range(0, items.Count)];
}
}
public void SelectSlot(ContainerSlot s)
{
_selected = s;
_selectedIndex = System.Array.IndexOf(_slots, s);
Debug.Log("_selectedIndex equal to " + _selectedIndex);
UpdateButtons();
}
public void OnOpen()
{
foreach(ContainerSlot slot in _slots)
{
slot.icon.sprite = default;
}
foreach(ContainerButtons button in _buttons)
{
button.gameObject.SetActive(false);
}
if (!_looted)
{
AddLoot(loot);
_looted = true;
}
for (int i = 0; i < _slots.Length; i++)
{
if (_items[i] != null)
{
_slots[i].icon.sprite = _items[i].icon;
}
}
}
public void UpdateButtons()
{
if (_items[_selectedIndex] != null)
{
Debug.Log("left button should be enabled");
_buttons[0].gameObject.SetActive(true);
_buttons[0].buttonText.text = "Take";
}
if(ContainsItems())
{
Debug.Log("right button should be enabled");
_buttons[1].gameObject.SetActive(true);
_buttons[1].buttonText.text = "Take All";
}
}
public bool ContainsItems()
{
for(int i = 0; i < _slots.Length; i++)
{
if (_items[i] != null)
return true;
}
return false;
}
}