- Home /
Why wont inventory wont update after or more purchases from in-game shop?
The problem: When I purchase 3 more items from the shop, one item will be be left blank. I have to go to the inventory then go back to the shop every 2 purchases or else one of the items wont show.
Here's the scripts:
public void BuildInventory()
{
for(loopInt = 0; loopInt <= inventoryContents.Count; loopInt++)
{
if(targetPath.transform.childCount >= inventoryContents.Count)
{
if(inventoryItems.Count >= 0)
{
}
}
else
{
NGUITools.AddChild(targetPath, itemPrefab);
foreach(Transform child in targetPath.transform)
{
if(child.gameObject.tag == "Item" && inventoryItems.Count <= inventoryContents.Count)
{
inventoryItems.Add(child.gameObject);
}
}
inventoryItems[loopInt].name = inventoryContents[loopInt].itemName;
inventoryItems[loopInt].GetComponent<InventoryItem>().item.itemName = inventoryContents[loopInt].itemName;
inventoryItems[loopInt].GetComponent<InventoryItem>().item.icon = inventoryContents[loopInt].icon;
inventoryItems[loopInt].GetComponent<InventoryItem>().UpdateInfo();
gridControlScript.Reposition();
}
}
Debug.Log("Inventory contains " + inventoryContents.Count + " items");
Debug.Log("Building inventory");
}
public void UpdateInfo()
{
itemNameTag = transform.Find("Item Text");
itemNameTag.GetComponent<UILabel>().text = item.itemName;
itemIcon = transform.Find("Texture");
itemIcon.GetComponent<UITexture>().material = item.icon;
}
And a screen shot:
Double check it is in fact your inventory at fault. SO you can deter$$anonymous$$e if it is a GUI displaying problem or simply your inventory management.
The inventory list shows up correctly in the inspector, and when I click the blank item, the info is blank too. So it's definitely a problem with my code.
Answer by Elroyman · Aug 11, 2013 at 12:47 AM
Just spit ballin here as I don't see quite how all the details are worked out outside of this but it seems to me that your first nested loop is being performed rather than kicking into the else statement there.
I suspect that this is due to the foreach loop reiterating through the list without having been cleared at the beginning. The first time it runs your foreach adds 1 to the list, then it adds 2 to the list on the second trip in through the parent loop. This means that on the third trip around, your inventoryContents.Count is 3 and so is the count on your inventoryitems (1 for the first time through, and then twice for the second time through, which counts 1 item twice).
I would set up a dummy list in the lower scope, then set that equal to the inventory items at the end or you else statement.
.
.
.
else
{
NGUITools.AddChild(targetpath, itemprefab);
[ListType] dummylist = new [ListType]();
foreach(Transform child in targetPath.transform)
{
if(child.gameObject.tag == "Item" && dummylist.Count <= inventoryContents.Count)
{
dummylist.Add(child.gameObject);
}
}
inventoryItem = dummylist;
That should get you a fresh count every time, rather than adding 1, then 2, then 3, then 4, etc. Which will keep you out of the top if statement until you reach your inventoryContents limit.
Hmm, this isn't working either, 1 more item shows up blank now.
ah i missed an edit...the if statement there in the middle should count the dummylist, not inventoryItems
Unfortunately it still has the same effect :( I'm not sure what to do at this point.
Your answer
Follow this Question
Related Questions
How to make a for loop display inventory correctly? 0 Answers
Help with Shop Script C# 0 Answers
Array index is out of range? 1 Answer
How to Show 'buy' button relevant to character selected ? 1 Answer