I don’t know if I am missing something simple or not, but here’s the situation:
What should happen is, entering the trigger should pop a button up in the middle of the screen that says “buy item for price”, and it get’s the item name and price from the other script that has an array of items in it. Clicking the button should add the item in question into a List of items, and display a button on the side of the screen with the item name. Clicking that button should remove the item from the List, apply the effect of the item, and delete the button.
If I start into the scene first, everything works as intended, however if I load into the scene from another scene, including starting in this one, loading into another scene, and then back to this one, the code does not work. The buy button appears, with the correct name, the Debug.Logs fires saying it added the item to the List, however in the inspector the item never gets added to the List, and because there’s nothing in the list the use buttons on the right side of the screen never appear.
Is there something I’m doing wrong, or another way to do this that does work after switching scenes?
The code on BuyingItems.cs:
void OnGUI()
{
if (canBuy == true)
{
if (GUI.Button (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 10, 200, 30), "Buy " + itemSold + " for " + itemPrice))
{
if(GameControl.control.money >= itemPrice)
{
if(ItemList.itemList.CheckForItem(itemID) == 1)
{
Debug.Log("Already have one");
}
else if (ItemList.itemList.CheckForItem(itemID) == 0)
{
GameControl.control.money -= itemPrice;
ItemList.itemList.AddItem(itemID);
Debug.Log("Buying " + itemSold);
}
}
}
}
}
The code on ItemInventory.cs:
public void AddItem(int itemID)
{
if (mainInventory.Contains (items[itemID]))
{
Debug.Log ("Item found. Can't add to mainInventory");
}
else
{
mainInventory.Add (items [itemID]);
Debug.Log ("Item not found. Adding item to main inventory");
}
}
public int CheckForItem(int itemID)
{
int hasItem;
if (mainInventory.Contains (items [itemID]))
{
hasItem = 1;
return hasItem;
}
else
{
hasItem = 0;
return hasItem;
}
}
// To draw on GUI
void OnGUI()
{
for (int i = 0; i < mainInventory.Count; i++)
{
if (GUI.Button (new Rect (Screen.width - 120, (Screen.height - Screen.height + 10) + (spacer * i), 115, 30), "Eat " + mainInventory*.itemName))*
-
{*
_ if(mainInventory*.effect == Item.Effect.Health)_
_ {_
_ GameControl.control.health += mainInventory.effectValue;
mainInventory.RemoveAt(i);
}*_
_ else if(mainInventory*.effect == Item.Effect.Mana)
{
GameControl.control.mana += mainInventory.effectValue;
mainInventory.RemoveAt(i);
}*_
* }*
* }*
What it should do:
[42874-working.jpg|42874]
What it does:
[42872-not-working.jpg*|42872]*
*
*