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

2D roguelike - item quipping - build not showing same as editor

Discussion in 'Scripting' started by Mallaboro, Jun 23, 2018.

  1. Mallaboro

    Mallaboro

    Joined:
    Sep 22, 2017
    Posts:
    18
    Hi there.

    I'm making a 2D rougelike and I've found when picking up items on the build they don't appear equipped on the character, and in the inventory 2 of the item are found. When the game is run in the editor however it works perfectly. 1 item picked up and if no item of that type is equipped, the item is automatically equipped.

    Can anyone think of a scenario where something could make this happen? I've shared some relevant snippets of code below but I wrote it over a year ago now and I'm contemplating redoing the whole system, but if it's not code related-- I don't have a graphics card, for instance.

    Any suggestions or advice would be nice.


    Code (CSharp):
    1.  
    2.     private void OnTriggerStay2D(Collider2D coll)
    3.     {
    4.         if (coll.gameObject.tag == "Player")
    5.         {
    6.             //Checks whether anything is hooked to the Pickup Event
    7.             if (PickedUp != null)
    8.             {
    9.                 //Sends the picked up items information to hooked classes
    10.                 if (PickedUp(m_itemName))
    11.                 {
    12.                     SoundDevice.instance.PlaySingle(sound);
    13.                     DestroyObject(gameObject);
    14.                 }
    15.             }
    16.         }
    17.     }
    Code (CSharp):
    1.  
    2. private bool PickUp(string name)
    3.     {
    4.         //get the xml item based off the picked up items name
    5.         PickupXML item = ItemManager.instance.GetIcon(name);
    6.  
    7.         //Search armour types to see if the respected armour types slot is empty so it can be worn on pickup
    8.         result = SetItemSlot(SlotType.Equipment, item, x => x.m_item == null && x.m_restrictedItemType == item.valItemType);      
    9.         if (result == false)
    10.             result = SetItemSlot(SlotType.Armoury, item, x => x.m_item == null);
    11.         if (result == false)
    12.             return false;
    13.         else
    14.             return true;
    15.     }
    16.  
    Code (CSharp):
    1.     public bool SetItemSlot(SlotType slotType, PickupXML item, System.Predicate<InventorySlot> match)
    2.     {
    3.         InventorySlot inventorySlot = null;
    4.  
    5.         switch (slotType)
    6.         {
    7.             case SlotType.Inventory:
    8.                 inventorySlot = m_inventorySlot.Find(match);
    9.                 break;
    10.             case SlotType.Equipment:
    11.                 inventorySlot = m_equipmentSlot.Find(match);
    12.                 break;
    13.             case SlotType.Armoury:
    14.                 inventorySlot = m_armourySlots.Find(match);
    15.                 break;
    16.         }
    17.  
    18.         if (inventorySlot == null) {
    19.             return false;
    20.         }
    21.         else {
    22.             inventorySlot.SetItem(item);
    23.             return true;
    24.         }
    25.     }
    26.  
    Code (CSharp):
    1.  
    2.     public void SetItem(PickupXML xmlItem)
    3.     {
    4.         //has information like the pickup icon file name, animation, damage properties and item type
    5.         m_item = xmlItem;
    6.  
    7.         //get this particular inventory slots array of sprites (each gear piece has a different rotation of frames depending on what the character is doing)
    8.         Sprite[] spriteBatch = m_item.GetAnimation();
    9.  
    10.         //gets the new item's frame equivilent of the item being replaced
    11.         var newSprite = Array.Find(spriteBatch, item => item.name == m_item.valSpriteName);
    12.  
    13.         m_image.sprite = newSprite;
    14.         m_text.text = m_item.valItemName;
    15.  
    16.         if(Equipment)
    17.         {  
    18.             if (Equip != null)
    19.             {
    20.                 Equip(m_item, ItemManager.instance.GetAnimation(m_item.valItemName));
    21.             }
    22.         }
    23.     }
    24.  
     
    Last edited: Jun 23, 2018
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    There's a few things that can trigger this. The most obvious one is if you are loading resources using something like System.IO.File routines rather than Resources.Load, or else the tried-and-true editor-dragged-in asset. In your code this might be in the ItemManager you are using. System.IO.File won't work on normal Unity assets in the build, but it will work in the editor.
     
  3. Mallaboro

    Mallaboro

    Joined:
    Sep 22, 2017
    Posts:
    18
    I've had a look and I'm using resources.load.

    All my XML files are kept in a streaming assets folder but I do use System.IO and System.Serialization when loading XML files, maybe that could be causing issues.
     
  4. Mallaboro

    Mallaboro

    Joined:
    Sep 22, 2017
    Posts:
    18
    Put the build in debug mode, found it wasn't able to access the animations xml file. Turned out it wasn't using utf-8.

    Problem solved, chuffed to bits. Happy days. ;)
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Excellent! Always feels good to get to the bottom of a problem. Thunder on forward my friend!
     
    Mallaboro likes this.