List object variable returning different results
I've been trying to figure this out for hours with no luck. It's probably something simple too.
I have a list of custom class objects and I'm trying to update a variable in a object. But for some reason when I get the variable from 2 different ways I'm getting 2 different results. This is not the case with any of the other variables in the object.
This is my inventory script. It creates empty items equal to the amount in the database list, then when I call AddItem() it should update an empty item with the new item stats. This part works, but I want to store the itemAmount and update this variable instead of creating a new item when AddItem() is called.
void Start()
{
inventory.Clear();
for (int i = 0; i < database.items.Count; i++)
{
inventory.Add(new Item()); // creates empty item with an id of -1
}
// Starting Items
AddItem(100);
AddItem(100);
AddItem(100);
AddItem(101);
AddItem(102);
AddItem(102);
}
// add item to inventory
public void AddItem(int p_id) {
Item itemToAdd = database.FetchItemByID(p_id);
// if item is already in inventory
if (IsItemInInventroy(itemToAdd) == true) {
for (int i = 0; i < inventory.Count; i++) {
if (inventory[i].itemID == p_id) {
inventory[i].itemAmount += 1;
inventoryMenu.CreateSlot(inventory[i], false);
mixingMenu.CreateSlot(inventory[i], true);
break;
}
}
}
// else add new item to inventory
else if (itemToAdd.itemID <= 199) // check if ingredient
{
for (int i = 0; i < inventory.Count; i++) {
if (inventory[i].itemID == -1) {
inventory[i] = itemToAdd;
inventory[i].itemAmount += 1;
inventoryMenu.CreateSlot(inventory[i], false);
mixingMenu.CreateSlot(inventory[i], true);
break;
}
}
}
}
This is from my inventoy menu script which handels drawing the UI on screen. I'm getting itemAmount twice in this script, but they dont return the same result. The CreateSlot function is called in the script above.
public void CreateSlot(Item p_item, bool p_isMixingMenu)
{
bool itemHasSlot = false;
for (int i = 0; i < slots.Count; i++)
{
if (p_item.itemID == slots[i].GetComponent<SlotScript>().slotData.slotID)
{
// increase inventory item count
int testInt = itemInventory.inventory[0].itemAmount; // returns 0 for some reason, it shouldnt
slots[i].transform.GetChild(0).GetChild(0).GetComponent<Text>().text = p_item.itemAmount.ToString(); // returns 3, the correct vaule
itemHasSlot = true;
}
}
Can you post your Item class please, depending on your default field/property settings(backing field) if each itemAmount was declared and initialized to -1 then the add/and operator(+=) shown in your above examples would set itemAmount to zero always since you're just stating itemAmount = itemAmount + itemAmount, anywho, lets see that class.
Answer by Waffle_Dragon · Nov 07, 2016 at 09:23 PM
This is the Item Class. The default constructor should be called for each item. Then the stats for the item are updated when I pass an item from the database list.
[System.Serializable]
public class Item {
public string itemName;
public int itemID;
public string itemDescription;
public string spritePath;
public int red;
public int green;
public int blue;
public int yellow;
public ItemType itemType;
public int itemValue;
[HideInInspector]
public int itemAmount;
[HideInInspector]
public bool hasBeenMade;
public enum ItemType {
Ingredient,
Potion,
Poison,
Enchantment,
Explosion
}
//constructor
public Item(string name, int id, int amount, string description, string sprite, int redNum, int greenNum, int blueNum, int yellowNum, ItemType type, int value) {
itemName = name;
itemID = id;
itemAmount = amount;
itemDescription = description;
red= redNum;
green = greenNum;
blue = blueNum;
yellow = yellowNum;
itemValue = value;
itemType = type;
spritePath = sprite;
hasBeenMade = false;
}
public Item()
{
itemID = -1;
itemAmount = 0;
hasBeenMade = false;
}
}
Your answer
Follow this Question
Related Questions
Problem with Lists and Remove 0 Answers
Define attacks to choose from 1 Answer
Custom class list seen as "none" in inspector. 1 Answer
Add object of specific width to a List 0 Answers
List suddenly auto-clear after WebRequest (in another Script)? 1 Answer