Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Dividing by Zero Error

Discussion in 'Scripting' started by UndeadMadison, May 27, 2020.

  1. UndeadMadison

    UndeadMadison

    Joined:
    Apr 16, 2020
    Posts:
    15
    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.



    Code (CSharp):
    1.     private RectTransform inventoryRect;
    2.  
    3.     public GameObject inventory;
    4.  
    5.     private float inventoryWidth, inventoryHeight;
    6.  
    7.     public int slots;
    8.     public int rows;
    9.  
    10.     public float paddingLeft, paddingTop;
    11.     public float slotSize;
    12.  
    13.     public GameObject slotPrefab;
    14.  
    15.     private List<GameObject> allSlots;
    16.  
    17.     void Start()
    18.     {
    19.         CreateLayout();
    20.     }
    21.  
    22.     private void CreateLayout()
    23.     {
    24.        /*  allSlots = new List<GameObject>();
    25.  
    26.         inventoryWidth = (slots / rows) * (slotSize + paddingLeft) + paddingLeft;
    27.         inventoryHeight = rows * (slotSize + paddingTop) + paddingTop;
    28.  
    29.         inventoryRect = GetComponent<RectTransform>();
    30.  
    31.         inventoryRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, inventoryWidth);
    32.         inventoryRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, inventoryHeight); */
    33.  
    34.         int columns = slots /rows;
    35.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    Add this right before your division:
    Code (CSharp):
    1. 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.
     
    Bunny83 and Joe-Censored like this.
  3. UndeadMadison

    UndeadMadison

    Joined:
    Apr 16, 2020
    Posts:
    15
    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!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    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.
     
    Joe-Censored likes this.
  5. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    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.

    this occurs 100% of the time
     
    ark1000000 and Kurt-Dekker like this.
  6. ark1000000

    ark1000000

    Joined:
    Jan 27, 2022
    Posts:
    1
    Thank you for this easy but not obvious solution! Strange bug
     
    shihooo and REDACT3D_ like this.