0

I have a variable slots of Type Slot which is a vector of 72 Slot, I made sure that none of the objects Slot is null, but my loop is stopping at the iteration 16 (in other words gridArray at position [1,9], can somebody help?

public class InventorySystem : MonoBehaviour
{
    [SerializeField] private Slot[] slots = new Slot[72];
    [SerializeField] private GameObject InventoryUI;

    private Slot[,] gridArray;

    // Defines the position of slots in the inventory grid
    private void DefinePosition(float panelWidth, float slotWidth, int[] panelPadding, float slotSpaceX)
    {
        float currentWidthOcupiedPerLine = 0;
        float gridWidth = panelWidth - panelPadding[0] - panelPadding[1];
        slotWidth = slotWidth + (slotSpaceX / 2);

        int numColumns = Mathf.FloorToInt(gridWidth / slotWidth);
        //int numRows = Mathf.CeilToInt((float)slots.Length / numColumns);
        int numRows = 0; // Inicializa o número de linhas como 0

        //calcules the number of  necessary rows 
        for (int i = 0; i < slots.Length; i++)
        {
            currentWidthOcupiedPerLine += slotWidth;

            if (currentWidthOcupiedPerLine >= gridWidth)
            {
                currentWidthOcupiedPerLine = 0;
                numRows++; // Incrementa o número de linhas
            }
        }

        Debug.Log(numColumns + ", " + numRows + "bau baun baun bauynbaunbaun hehe");

        // Inicializa o gridArray com as dimensões corretas
        gridArray = new Slot[numRows, numColumns];
 
        int row = 0;
        int column = -1;

        Debug.Log(slots.Length + "caucau " + "asas");

        for (int i = 0; i < slots.Length; i++)
        {
            currentWidthOcupiedPerLine += slotWidth;

            if (currentWidthOcupiedPerLine >= gridWidth)
            {
                currentWidthOcupiedPerLine = 0;
                row++;
                column = 0;
                Debug.Log(string.Format("linha {0} {1}", row, column));
                gridArray[row, column] = slots[i];
                continue;
            }

            column++;
            Debug.Log(string.Format(" {0} {1}", row, column));
            gridArray[row, column] = slots[i];
        }
    }

    private void Awake()
    {
        for (int i = 0; i < slots.Length; i++)
        {
            if (slots[i].ItemInSlot == null)
            {
                slots[i].transform.GetChild(0).gameObject.SetActive(false);
            }
        }

        Debug.Log("a");
        DefinePosition(870, 90, new int[] { 10, 0 }, 5);
    }
}

In this linegridArray = new Slot[numRows, numColumns]; is being defined with 8 rows and 9 columns

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jamelaumn
  • 37
  • 6
  • `1,9` is out of range. Indices are zero based. – Retired Ninja Jun 03 '23 at 22:15
  • is there a way to fix that? Im trying to do this so I can give cardial positions to my slot – Jamelaumn Jun 03 '23 at 22:18
  • @Jamelaumn Have a look at this thread: https://stackoverflow.com/questions/5728409/how-to-create-a-1-dimensional-array-in-c-sharp-with-index-starting-at-1 <- I personally wouldn't recommend it. I think it'd be easier / less code to just adjust your thinking and shift to a zero based index. Either that or you can write a custom wrapper class that has the array as zero based internally, shift all interactions with it by 1. – B.O.B. Jun 03 '23 at 22:44
  • to be frank I found it quite complex haha, but I need to kinda make this grid so that I can give X and Y postions to my slot, because Im trying to build a game where my item can ocuppy more than one slot in size(like the inventory of escape from tarkov or resident eveil 4), so I kinda needed to give the cardial positions T-T – Jamelaumn Jun 03 '23 at 23:08

0 Answers0