how can i update the value of a variable that is in a prefab button without duplicating the prefab
Hi all, So my aim i was trying to make a shop for where a player can buy items for his car. The Shop contains 6 different types of items and variety of them. After the player click on the prefab button I want the Prefab to appear in the Player's storage. So far i managed to populate the lists and successfully made the prefabs to appear in the storage. By using the updateStorage() function made it duplicates all the prefabs that are already bought. That is normal i was checking if the code works but how can i fix that so the Items that are already in the storage doesn't duplicate but the amount (count) of those parts increase 10 times each time. E.g each time the player clicks on the prefab button he gets x10 uses of that part if he clicks again i want the prefab button to stay one but have x20 uses of part. Any ideas , thoughts or coding help would be muchappreciate.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ShopHandler : MonoBehaviour {
[System.Serializable]
public class Part
{
public string name;
public Sprite image;
public string description;
public int cost;
public float gain;
public int count;
}
public GameObject[] buttonParts;
public GameObject TyreContainer, FrontWingContainer, RearWingContainer, SuspensionContainer, EngineContainer, SidepodContainer;
public GameObject TyreContainerStorage, FrontWingContainerStorage, RearWingContainerStorage, SuspensionContainerStorage, EngineContainerStorage, SidepodContainerStorage;
public Part[] TyreTypes,FrontWingTypes,RearWingTypes,SuspensionTypes,SidepodsTypes,EngineTypes;
public GameObject ShopItemPrefab;
int counter;
// Use this for initialization
void Start() {
pupulateShop(TyreTypes, TyreContainer);
pupulateShop(FrontWingTypes, FrontWingContainer);
pupulateShop(RearWingTypes, RearWingContainer);
pupulateShop(EngineTypes, EngineContainer);
pupulateShop(SuspensionTypes, SuspensionContainer);
pupulateShop(SidepodsTypes, SidepodContainer);
updateStorage();
// so it can load any parts that are already in storage
}
// In order to move the parts to the storage
void updateStorage() {
LoadStorage(TyreTypes, TyreContainerStorage);
LoadStorage(FrontWingTypes, FrontWingContainerStorage);
LoadStorage(RearWingTypes, RearWingContainerStorage);
LoadStorage(EngineTypes, EngineContainerStorage);
LoadStorage(SuspensionTypes, SuspensionContainerStorage);
LoadStorage(SidepodsTypes, SidepodContainerStorage);
}
public void pupulateShop(Part[] PartTypes,GameObject container)
{
buttonParts = new GameObject[PartTypes.Length];
Debug.Log("PartType loaded");
counter = 0;
foreach (Part i in PartTypes)
{
GameObject btn = Instantiate(ShopItemPrefab) as GameObject;
buttonParts[counter] = btn;
ItemScript scp = btn.GetComponent<ItemScript>();
scp.name.text = i.name;
scp.cost.text = i.cost.ToString();
scp.count.text = i.count.ToString();
scp.gain.text = i.gain.ToString();
scp.icon.sprite = i.image;
scp.description.text = i.description;
Part thisPart = i;
scp.btnPartBuy.onClick.AddListener(() => Purchase(thisPart));
btn.transform.SetParent(container.transform, false);
counter++;
}
}
void LoadStorage(Part[] PartTypes, GameObject containerStorage)
{
buttonParts = new GameObject[PartTypes.Length];
counter = 0;
foreach (Part i in PartTypes)
{
if (i.count != 0)
{
GameObject btn = Instantiate(ShopItemPrefab) as GameObject;
buttonParts[counter] = btn;
ItemScript scp = btn.GetComponent<ItemScript>();
scp.name.text = i.name;
scp.cost.text = i.cost.ToString();
scp.count.text = i.count.ToString();
scp.gain.text = i.gain.ToString();
scp.icon.sprite = i.image;
scp.description.text = i.description;
Part thisPart = i;
scp.btnPartBuy.onClick.AddListener(() => SelectItemToUse(thisPart));
btn.transform.SetParent(containerStorage.transform, false);
counter++;
}
}
}
void Purchase(Part bought) {
bought.count = bought.count + 10;
Debug.Log("Item bought");
Debug.Log(bought.count);
updateStorage();
}
// Update is called once per fram
void SelectItemToUse(Part PartSelected)
{
Debug.Log("Item Selected");
}
}
Your answer
Follow this Question
Related Questions
Change the dropping prefab for each in-game drop 1 Answer
Unity Inventory Database ID Error 1 Answer
How to add item to inventory with these scripts 0 Answers
Save gameobjects in a list before destoying 0 Answers
Modifiable shop content 0 Answers