Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Int is not interpreted as int

Discussion in 'Scripting' started by meierdesigns_unity, May 5, 2020.

  1. meierdesigns_unity

    meierdesigns_unity

    Joined:
    Apr 16, 2020
    Posts:
    7
    Hey

    I am trying to get an int into an if condition. But I get this null pointer error all the time:

    NullReferenceException: Object reference not set to an instance of an object
    InventoryTutorial.addItemSSS (System.Int32 x, System.Int32 y, Item item) (at Assets/Inventory/InventoryTutorial.cs:181)
    InventoryTutorial.detectMouseAction () (at Assets/Inventory/InventoryTutorial.cs:261)
    InventoryTutorial.detectGUIAction () (at Assets/Inventory/InventoryTutorial.cs:196)
    InventoryTutorial.OnGUI () (at Assets/Inventory/InventoryTutorial.cs:107)

    The code snippet the error comes from is the following:

    Code (CSharp):
    1.  
    2.  
    3.     void addItem(int x, int y, Item item)
    4.     {
    5.         for (int sX = 0; sX < item.width; sX++)
    6.         {
    7.             for (int sY = 0; sY < item.height; sY++)
    8.             {
    9.                 if (slots[x, y].occupied)
    10.                 {
    11.                     // Debug.Log("breaks " + x + ", " + y);
    12.  
    13.                     return;
    14.                 }
    15.             }
    16.         }
    17.  
    18.         if (x + item.width > slotWidthSize)
    19.         {
    20.             Debug.Log("Item out of X bounds!");
    21.             return;
    22.         }
    23.  
    24.         else if (y + item.height > slotWidthSize)
    25.         {
    26.             Debug.Log("Item out of Y bounds!");
    27.             return;
    28.         }
    29.  
    30.         item.x = x;
    31.         item.y = y;
    32.  
    33.         // Debug.Log("Coordinates are " + x + ", " + y);
    34.  
    35.         items.Add(item);
    36.  
    37.         for (int sX = x; sX < item.width + x; sX++)
    38.         {
    39.             for (int sY = y; sY < item.height + y; sY++)
    40.             {
    41.                 slots[sX, sY].occupied = true;
    42.             }
    43.         }
    44.     }
    45.  
    46.  
    47.                       if (secondSelected.x != selected.x || secondSelected.y != selected.y)
    48.                         {
    49.                             // dragItem();
    50.                             Debug.Log("secondSelected.x in int is: " + (int)secondSelected.x + ", and secondSelected.y in int is: " + (int)secondSelected.y);
    51.                             addItem((int)secondSelected.x, (int)secondSelected.y, temp);
    52.  
    53.                         }
    54.  
    55.  
    56.  
    Why is the int not interpreted as such and gives me this null pointer exeption?
     
  2. GladStudio

    GladStudio

    Joined:
    Mar 7, 2016
    Posts:
    16
    On line 24, you use the width variable instead of the height. Perhaps this is not the reason, but there is such a mistake.
     
    meierdesigns_unity likes this.
  3. meierdesigns_unity

    meierdesigns_unity

    Joined:
    Apr 16, 2020
    Posts:
    7
    Wow just skipped that point. now I see it. it fixed the problem. Thank you, GladStudio.