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. Dismiss Notice

[SOLVED][C#] Internal compiler error

Discussion in 'Scripting' started by PeterR, Jul 15, 2014.

  1. PeterR

    PeterR

    Joined:
    Jul 15, 2014
    Posts:
    15
    I have no idea how to go about troubleshooting this. It just started happening while I was working on an inventory script for my game. Any clues would be much appreciated!

    Here is the full error:
    Here is my script:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System;
    6.  
    7. public class Inventory : MonoBehaviour {
    8.  
    9.     #region Variable Declarations
    10.     public List<Item> items;
    11.     public List<InventorySlot> storage_slots;
    12.     public List<InventorySlot> equip_slots;
    13.     public List<InventorySlot> all_slots;
    14.     private int storage_slot_count = 10;
    15.  
    16.     public GUISkin skin;
    17.     private bool _show_inventory;
    18.     private bool _dragging_item;
    19.     private Item _drag_item;
    20.     private int _dragged_slot_ID;
    21.     private WorldControl world;
    22.     private ItemDatabase item_database;
    23.     private int _STORGE_SLOT_X = 10;
    24.     private int _STORGE_SLOT_Y = 10;
    25.     private int _EQUIP_SLOT_X = 200;
    26.     private int _EQUIP_SLOT_Y = 10;
    27.     private int _SLOT_WIDTH = 50;
    28.     private int _SLOT_HEIGHT = 50;
    29.     #endregion
    30.  
    31.     #region Inventory Constructors
    32.     public Inventory(List<int> item_IDs, List<InventorySlot> storage, List<InventorySlot> equip)
    33.     {
    34.         storage_slots = storage;
    35.         equip_slots = equip;
    36.         foreach(int id in item_IDs)
    37.         {
    38.             Item current_item = item_database.get_item_by_ID(id);
    39.             if(current_item != null)
    40.             {
    41.                 items.Add(current_item);
    42.             }
    43.         }
    44.     }
    45.  
    46.     public Inventory()
    47.     {
    48.         items = new List<Item> ();
    49.         storage_slots = new List<InventorySlot>();
    50.         equip_slots = new List<InventorySlot>();
    51.     }
    52.  
    53.     #endregion
    54.  
    55.     #region MonoBehaviour functions
    56.     void Awake()
    57.     {
    58.         world = (WorldControl)GameObject.Find("world_control").GetComponent<WorldControl>();
    59.         item_database = world.item_database;
    60.     }
    61.  
    62.     void Start()
    63.     {
    64.         build_slots();
    65.         add_item(0);
    66.     }
    67.  
    68.     void Update()
    69.     {
    70.         if (Input.GetButtonDown ("Inventory") && gameObject.tag == "Player")
    71.         {
    72.             if (_show_inventory)
    73.             {
    74.                 _show_inventory = false;
    75.                 world.unpause_game();
    76.             }else{
    77.                 _show_inventory = true;
    78.                 world.pause_game();
    79.             }
    80.         }
    81.     }
    82.  
    83.     void OnGUI(){
    84.         if(_show_inventory)
    85.         {
    86.             draw_inventory();
    87.         }
    88.         if (_dragging_item)
    89.         {
    90.             GUI.DrawTexture(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50), _drag_item.item_icon);
    91.         }
    92.     }
    93.     #endregion
    94.  
    95.     #region Public API
    96.  
    97.     public bool check_inventory(int ID)
    98.     {
    99.         foreach (Item item in items)
    100.         {
    101.             if (item.item_ID == ID)
    102.             {
    103.                 return true;
    104.             }
    105.         }
    106.         return false;
    107.     }
    108.  
    109.     public void add_item(int ID)
    110.     {
    111.         Item current_item = item_database.get_item_by_ID (ID);
    112.         InventorySlot next_slot;
    113.         next_slot = get_next_slot(storage_slots, current_item.item_ID);
    114.         if(current_item != null && next_slot != null)
    115.         {
    116.             items.Add(current_item);
    117.             next_slot.add_item(current_item);
    118.         }
    119.     }
    120.  
    121.     public void remove_item(int ID, int slot_ID)
    122.     {
    123.         Item current_item = get_item_by_ID(ID);
    124.         InventorySlot current_slot = get_slot_by_ID(storage_slots, slot_ID);
    125.         if(current_item != null && current_slot != null && current_slot.slot_item.item_ID == ID)
    126.         {
    127.             items.Remove(current_item);
    128.             current_slot.remove_item();
    129.         }
    130.     }
    131.  
    132.     public InventorySlot get_slot_by_ID(List<InventorySlot> slots, int ID)
    133.     {
    134.         foreach(InventorySlot slot in slots)
    135.         {
    136.             if(slot.slot_ID == ID)
    137.             {
    138.                 return slot;
    139.             }
    140.         }
    141.         return null;
    142.     }
    143.  
    144.     // find the first slot that contains the specified item ID
    145.     public InventorySlot get_slot_containing(List<InventorySlot> slots, int ID)
    146.     {
    147.         foreach(InventorySlot slot in slots)
    148.         {
    149.             if(slot.slot_item.item_ID == ID)
    150.             {
    151.                 return slot;
    152.             }
    153.         }
    154.         return null;
    155.     }
    156.  
    157.     // Remove all items of the specified item type from inventory
    158.     public void remove_all_item(int ID)
    159.     {
    160.         clear_slots_by_item_ID(storage_slots, ID);
    161.         clear_slots_by_item_ID(equip_slots, ID);
    162.         foreach(Item each_item in items)
    163.         {
    164.             if(each_item.item_ID == ID)
    165.             {
    166.                 items.Remove(each_item);
    167.             }
    168.         }
    169.     }
    170.  
    171.     public void clear_inventory()
    172.     {
    173.         items = new List<Item> ();
    174.         build_slots();
    175.     }
    176.  
    177.     public Item get_item_by_ID(int ID)
    178.     {
    179.         foreach(Item each_item in items)
    180.         {
    181.             if(each_item.item_ID == ID)
    182.             {
    183.                 return each_item;
    184.             }
    185.         }
    186.         return null;
    187.     }
    188.  
    189.     public int get_item_count(int ID)
    190.     {
    191.         int i = 0;
    192.         foreach(Item each_item in items)
    193.         {
    194.             if(each_item.item_ID == ID)
    195.             {
    196.                 i += 1;
    197.             }
    198.         }
    199.         return i;
    200.     }
    201.     #endregion
    202.  
    203.     #region Private Functions
    204.     // get the next available slot to add an item to the inventory. [For stacking items]
    205.     private InventorySlot get_next_slot(List<InventorySlot> slots, int stack_item=-1)
    206.     {
    207.         if(stack_item >= 0)
    208.         {
    209.             Item item = item_database.get_item_by_ID(stack_item);
    210.             if(item.item_stackable)
    211.             {
    212.                 foreach(InventorySlot slot in slots)
    213.                 {
    214.                     if(slot.slot_item.item_ID == stack_item)
    215.                     {
    216.                         return slot;
    217.                     }
    218.                 }
    219.             }
    220.         }
    221.         foreach(InventorySlot slot in slots)
    222.         {
    223.             if(slot.slot_available)
    224.             {
    225.                 return slot;
    226.             }
    227.         }
    228.         return null;
    229.     }
    230.  
    231.     private void clear_slots_by_item_ID(List<InventorySlot> slots, int ID)
    232.     {
    233.         foreach(InventorySlot slot in slots)
    234.         {
    235.             if(slot.slot_item.item_ID == ID)
    236.             {
    237.                 slot.clear_slot();
    238.             }
    239.         }
    240.     }
    241.  
    242.     private int get_next_slot_ID(List<InventorySlot> slots)
    243.     {
    244.         int i;
    245.         for(i = 0;i < 10000; i++)
    246.         {
    247.             foreach(InventorySlot slot in slots)
    248.             {
    249.                 if(slot.slot_ID == i)
    250.                 {
    251.                     i += 1;
    252.                     continue;
    253.                 }
    254.             }
    255.         }
    256.         return i;
    257.     }
    258.  
    259.     private void build_slots()
    260.     {
    261.         storage_slots = new List<InventorySlot>();
    262.         equip_slots = new List<InventorySlot>();
    263.         all_slots = new List<InventorySlot>();
    264.         InventorySlot new_slot;
    265.         int slot_id = 0;
    266.         Rect new_slot_rect;
    267.         for(int i = 0; i < storage_slot_count; i++)
    268.         {
    269.             new_slot_rect =  new Rect(_STORGE_SLOT_X, _STORGE_SLOT_Y + (i * _SLOT_HEIGHT), _SLOT_WIDTH, _SLOT_HEIGHT);
    270.             new_slot = new InventorySlot(ID: slot_id, data: item_database, input_skin: skin, rect: new_slot_rect);
    271.             storage_slots.Add(new_slot);
    272.             all_slots.Add(new_slot);
    273.             slot_id += 1;
    274.         }
    275.         var slot_types = Enum.GetValues(typeof(EquipSlot.EquipSlotType));
    276.         int index = 0;
    277.         foreach(EquipSlot.EquipSlotType type in slot_types)
    278.         {
    279.             if(type != EquipSlot.EquipSlotType.None)
    280.             {
    281.                 new_slot_rect =  new Rect(_EQUIP_SLOT_X, _EQUIP_SLOT_Y + (index * _SLOT_HEIGHT), _SLOT_WIDTH, _SLOT_HEIGHT);
    282.                 new_slot =  new InventorySlot(ID: slot_id, data: item_database, input_skin: skin, type: type, rect: new_slot_rect);
    283.                 equip_slots.Add(new_slot);
    284.                 all_slots.Add(new_slot);
    285.                 slot_id += 1;
    286.                 index += 1;
    287.             }
    288.         }
    289.     }
    290.     #endregion
    291.  
    292.     void draw_inventory()
    293.     {
    294.         Event current_event = Event.current;
    295.         foreach(InventorySlot slot in all_slots)
    296.         {
    297.             slot.draw_slot();
    298.             if(slot.slot_item != null)
    299.             {
    300.                 //If the mouse is in the screen space of the slot
    301.                 if (slot.slot_rect.Contains(current_event.mousePosition))
    302.                 {
    303.                     // If you grab the item from this slot
    304.                     if (current_event.button == 0 && current_event.type == EventType.mouseDrag && !_dragging_item)
    305.                     {
    306.                         _dragging_item = true;
    307.                         _dragged_slot_ID = slot.slot_ID;
    308.                         _drag_item = slot.slot_item;
    309.                         slot.clear_slot();
    310.                         slot.draw_slot();
    311.                     }
    312.                     // If you're dragging an item and you let go of the mouse button
    313.                     // and this slot has an item, swap the items.
    314.                     if (current_event.type == EventType.mouseUp && _dragging_item)
    315.                     {
    316.                         InventorySlot previous_slot = get_slot_by_ID(all_slots, _dragged_slot_ID);
    317.                         previous_slot.clear_slot();
    318.                         previous_slot.draw_slot();
    319.                         slot.add_item(_drag_item.item_ID);
    320.                         _dragging_item = false;
    321.                         _drag_item = null;
    322.                         slot.draw_slot();
    323.                     }
    324.                 }
    325.                 // If the current slot is empty
    326.             } else {
    327.                 if(slot.slot_rect.Contains(current_event.mousePosition))
    328.                 {
    329.                     if (current_event.type == EventType.mouseUp && _dragging_item)
    330.                     {
    331.                         slot.add_item(_drag_item.item_ID);
    332.                         _dragging_item = false;
    333.                         _drag_item = null;
    334.                         slot.draw_slot();
    335.                     }
    336.                 }
    337.             }
    338.         }
    339.     }
    340. }
    341.  
    342. public class Slot{
    343.     private int _slot_ID;
    344.     public int slot_ID {get{return _slot_ID;}}
    345.     public bool slot_available = true;
    346.     public Rect slot_rect;
    347.     public string slot_style;
    348.     public string slot_label;
    349.     public GUISkin slot_skin;
    350.  
    351.     public Slot(int ID, Rect rect, bool available=true, string style="inventory_slot", string label="", GUISkin skin=null)
    352.     {
    353.         _slot_ID = ID;
    354.         slot_available = available;
    355.         slot_rect = rect;
    356.         slot_style = style;
    357.         slot_label = label;
    358.         slot_skin = skin;
    359.     }
    360.  
    361.     public void draw_slot()
    362.     {
    363.         GUI.Box(slot_rect, slot_label, slot_skin.GetStyle(slot_style));
    364.     }
    365. }
    366.  
    367. public class InventorySlot : Slot{
    368.     public Item slot_item;
    369.     public int slot_item_count;
    370.     public ItemDatabase item_database;
    371.  
    372.     public InventorySlot(int ID, Rect rect, bool available=true, string style="inventory_slot", string label="", GUISkin skin=null, ItemDatabase data=null, Item item=null, int count=0)
    373.         :base(ID, rect, slot_available, style, label, skin)
    374.     {
    375.         if(slot_item == null)
    376.             slot_item = new Item();
    377.         else
    378.             slot_item = item;
    379.         if(data != null)
    380.             item_database = data;
    381.         if(slot_item.item_name != null && count == 0)
    382.             slot_item_count = 1;
    383.         else
    384.             slot_item_count = count;
    385.     }
    386.  
    387.     //#region Public API
    388.  
    389.     public void add_item(int ID)
    390.     {
    391.         if(slot_available && item_database.check_database(ID))
    392.         {
    393.             Item item = item_database.get_item_by_ID(ID);
    394.             if (item != null)
    395.             {
    396.                 if (slot_item == null || slot_item.item_ID != ID)
    397.                 {
    398.                     slot_item = item;
    399.                 }else if(slot_item.item_ID == ID && slot_item.item_stackable)
    400.                 {
    401.                     slot_item_count += 1;
    402.                 }
    403.             }
    404.         }
    405.     }
    406.  
    407.     public void add_item(Item item)
    408.     {
    409.         if(slot_available && item_database.check_database(item.item_ID))
    410.         {
    411.             if (item != null)
    412.             {
    413.                 if (slot_item.item_name == null || slot_item.item_ID != item.item_ID)
    414.                 {
    415.                     slot_item = item;
    416.                 }else if(slot_item.item_ID == item.item_ID && slot_item.item_stackable)
    417.                 {
    418.                     slot_item_count += 1;
    419.                 }
    420.             }
    421.         }
    422.     }
    423.  
    424.     public void remove_item()
    425.     {
    426.         if(slot_item != null)
    427.         {
    428.             if(slot_item_count > 1)
    429.             {
    430.                 slot_item_count -= 1;
    431.             }else{
    432.                 slot_item = new Item();
    433.             }
    434.         }
    435.     }
    436.  
    437.     public void clear_slot()
    438.     {
    439.         slot_item = new Item();
    440.     }
    441.  
    442.     new public void draw_slot()
    443.     {
    444.         GUI.Box(slot_rect, slot_label, slot_skin.GetStyle(slot_style));
    445.    
    446.         if(slot_item.item_icon != null)
    447.         {
    448.             GUI.DrawTexture(slot_rect, slot_item.item_icon);
    449.         }
    450.     }
    451. }
    452.  
    453. public class EquipSlot : InventorySlot{
    454.     public enum EquipSlotType {
    455.         None,
    456.         Head,
    457.         Torso,
    458.         Left_Hand,
    459.         Right_Hand,
    460.         Legs,
    461.         Feet,
    462.         Ammo,
    463.         Consumable}
    464.     public bool item_equipped;
    465.  
    466.     public EquipSlot(int ID, Rect rect, bool available, string style, string label, GUISkin skin, ItemDatabase data, Item item, int count, EquipSlotType type=EquipSlotType.None)
    467.         :base(ID, rect, slot_available, style, label, skin, data, item, count)
    468.     {
    469.         if(slot_item.item_name != null)
    470.         {
    471.             if(type == EquipSlotType.None)
    472.             {
    473.                 type = item.item_slot;
    474.                 item_equipped = true;
    475.             }
    476.         }else{
    477.             item_equipped = false;
    478.         }
    479.     }
    480.  
    481.     private void equip_item(Item item, EquipSlot.EquipSlotType type)
    482.     {
    483.         /*
    484.         if(type == EquipSlot.EquipSlotType.Left_Hand)
    485.         {
    486.             GameObject new_prefab = (GameObject)Instantiate(item.item_prefab);
    487.             new_prefab.transform.parent = GameObject.Find("equip_slot_l_hand").GetComponent<Transform>();
    488.         }
    489.         */
    490.     }
    491. }
    492.  
    SOLVED
    The issue was that Unity does not support named parameters. Which are used on lines 270 and 282 in the above code.
     
    Last edited: Jul 16, 2014
  2. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    Mono sucks, sorry to not fully answer the question, but it has a lot of issues. There could be nothing wrong with it, it just messes up...a lot! Looking through your code there might be an issue with the EquipSlot class in your constructor. You aren't setting your parameters types.
     
  3. PeterR

    PeterR

    Joined:
    Jul 15, 2014
    Posts:
    15
    Thanks for the Response Dex. Could you elaborate on this?
    Which parameter types am I not setting?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    This is a pointless response. I've never received errors like this.

    First thing I would say is that you're creating a bunch of constructors for a class that inherits from MonoBehaviour and overriding the parameterless one. Unity probably isn't going to react well to that given how it builds new MonoBehaviour instances. Besides that, it's pointless because you won't be able to use them in your own code anyway.
     
  5. PeterR

    PeterR

    Joined:
    Jul 15, 2014
    Posts:
    15
    Good point Kelso, that first constructor for my Inventory class is useless. I am not used to the way Unity instantiates MonoBehavior classes as components on game objects. I made that first constructor on auto-pilot.

    I will remove it this evening, but I do not believe it will solve my problem. A quite old version of this script has the same constructor and it did not produce any compiler errors.
     
  6. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    Mono always pumps out weird errors for me, I had to reset Mono constantly. Maybe I installed it wrong but I have had issues since day 1 of using it. I use VS now and haven't had the issues I had before with it. Anyways Fractured State looks amazing, good luck with it!
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You can't use constructors on MonoBehaviours anyway so there's no point in having them in the first place.

    Are you talking about Mono or MonoDevelop?
     
  8. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    The IDE.
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    His error is from the compiler which is IDE agnostic. I'll agree with you in disliking MonoDevelop though.
     
    DexRobinson likes this.
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    MonoDevelop is not Mono.
     
  11. PeterR

    PeterR

    Joined:
    Jul 15, 2014
    Posts:
    15
    As I suspected, removing the inventory class constructors did not resolve the issue, but I'm glad you pointed out that they are pointless anyway. Any other ideas?
     
  12. jesliwang

    jesliwang

    Joined:
    Apr 1, 2013
    Posts:
    4
    thansk, this help me a lot!!
     
  13. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,911
    Isn't this Q obsolete? Unity as of at least 5.x seems fine with named parameters.
     
    TaleOf4Gamers likes this.