- Home /
Transform child out of bounds
I am working on a inventory system and am having a transform child out of bounds error. my Hierarchy:
>InventoryCanavas
---->Hotbar
-------->Slot (1)
-------->Slot (2)
---------------->CubeIMG
---------------------->Cube
------- >Slot (3)
-------- >Slot (4)
-------- >Slot (5)
-------- >Slot (6)
-------- >Slot (7)
-------- >Slot (8)
-------- >Slot (9)
-------- >Slot (10)
--END--
so the way it works is index is set by the scroll wheel (I am sure index is correct I have done a lot of testing on it). index then tells this what slot to go to and then takes the child and sends it to my placement script with some data that is found by comparing the child to a list of placeable objects in the game. It the cooses from BLOCK SAW DRILL WELDER. My debug says the error is in the line "placement.objToPlace = hotbar.transform.GetChild(index).GetChild(0).GetChild(0).gameObject;" the first child of the slot object is to add a graphic to the slot. the code:
f(hotbar.transform.GetChild(index).transform.childCount == 1)
{
placement.objToPlace = hotbar.transform.GetChild(index).GetChild(0).GetChild(0).gameObject;
for (int i = 0; i < AllObjects.Length; i++)
{
if (hotbar.transform.GetChild(index).GetChild(0).gameObject.name == AllObjects[i].name)
{
Debug.Log("This thing that I am holding has the data: " + AllObjects_DATA[i]);
placement.type = AllObjects_DATA[i];
}
}
}
Answer by ray2yar · Jul 11, 2020 at 07:10 PM
So one part of that lookup you are trying to get a child index that is greater than what is there. A few things to try: first making sure the 'index' isn't going to high, even though your slots start at '1' the child will start at 0. You could test this by hard coding a certain index and making sure it actually looks something up. Since you are dealing with an inventory system is it possible that a slot you are looking at is actually empty and therefore has no children?
I know the index value is correct because I color the slot to match the index, and I know my slots start at 1 and getchild starts at 0. and the top if statements checks to make sure there is a child before reading it and sending it to my placement scrip.
Just checking my understanding - so the player selects an item to place in your hotbar. Then to select where it goes they scroll through. Presumably they select a spot that currently isn't occupied- right? So there shouldn't be children in the slot because its empty?
no ok so the way it works is you can click around items between the inventory and the hotbar (sorta like $$anonymous$$inecraft) then use the scroll wheel to select each slot in the hotbar. then if that slot is not empty ( if(hotbar.transform.GetChild(index).transform.childCount == 1))it sends the contents to my placement script.