I am following a tutorial on creation of inventories but for some reason when I do what he did it brings up the exception error. I have retyped it a few times and tried to figure it out by Serilizing some things. I ended up commenting out the code that I wasn’t going to keep that is if by the end of the tutorial I am able to delete it without messing things up. I know it is with the slots / rows part in the code but I don’t know what the problem with it is. Here is my code. In the inspector I did put rows = 1 and slots = 5 just for texting and no matter what number I put in it still does it.
private RectTransform inventoryRect;
public GameObject inventory;
private float inventoryWidth, inventoryHeight;
public int slots;
public int rows;
public float paddingLeft, paddingTop;
public float slotSize;
public GameObject slotPrefab;
private List<GameObject> allSlots;
void Start()
{
CreateLayout();
}
private void CreateLayout()
{
/* allSlots = new List<GameObject>();
inventoryWidth = (slots / rows) * (slotSize + paddingLeft) + paddingLeft;
inventoryHeight = rows * (slotSize + paddingTop) + paddingTop;
inventoryRect = GetComponent<RectTransform>();
inventoryRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, inventoryWidth);
inventoryRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, inventoryHeight); */
int columns = slots /rows;
}
Add this right before your division: Debug.Log($"I am {this.name}. Slots is {slots}, rows is {rows}");Then run the code and check the console.
How many times does this get printed? Once? More than once? What are the names of the game objects that print them?
If this prints that rows is 0, you’ve missed something in your scene. Perhaps a stray object with rows set to 0.
Thank you! For some reason I had it on the actual holder of the inventory and didn’t realize it. I didn’t even know you could do that in code to check. I will have to do that more often. Again thank you!
I’ve done this about a hundred times: usually it is because you just barely miss it when you drag it onto a GameObject. The “print my name” debug step that Praetor suggested above is an awesome approach.
I get the Divide by zero error when I’m working with the Terrain system. Basically if you try to run the game while the terrain is selected (orange highlight) you’ll get the error and the game will be paused.
Solution is to click outside the editor window to clear the selection before pressing run.