- Home /
Shop system isn’t working and I don’t know why
So I recently tried to make a shop system using foreach but cant get it to work the buttons won’t change except one, they all work when I buy them, but not when I equip or unequip an item if would be great if someone can take it look and tell me what’s wrong please and thank you
using MoreMountains.NiceVibrations;
using UnityEngine;
using UnityEngine.Events;
using TMPro;
using EasyMobile;
using System.Collections.Generic;
using UnityEngine.Serialization;
public class ShopManager : MonoBehaviour
{
[FormerlySerializedAs("NotEnoughText")] [SerializeField]
private UnityEvent notEnoughText;
private float _playerMoney;
private int _itemValueRainbowSkin;
private int _itemValueGrayTrail;
private int _itemValueGreenTrail;
private int _itemValueRainbowTrail;
private int _itemValueGreenSkin;
public List<ShopItem> shopItems;
[System.Serializable]
public class ShopItem
{
public string itemName;
public GameObject buyButton;
public GameObject equipButton;
public GameObject unequipButton;
[FormerlySerializedAs("Cost")] public float cost;
public TextMeshProUGUI costText;
}
// Start is called before the first frame update
private void Start()
{
CheckButtons();
}
public void UnEquip(string itemName)
{
_itemValueRainbowSkin = PlayerPrefs.GetInt("ItemValueRainbowSkin");
_itemValueGrayTrail = PlayerPrefs.GetInt("ItemValueGrayTrail");
_itemValueGreenTrail = PlayerPrefs.GetInt("ItemValueGreenTrail");
_itemValueGreenSkin = PlayerPrefs.GetInt("ItemValueGreenSkin");
_itemValueRainbowTrail = PlayerPrefs.GetInt("ItemValueRainbowTrail");
foreach (var item in shopItems)
{
#region RainbowSkin
if (item.itemName == itemName)
{
if (_itemValueRainbowSkin > 1)
{
PlayerPrefs.SetInt("ItemValueRainbowSkin", 1);
CheckButtons();
return;
}
}
#endregion
#region GreenSkinUnEquip
if (item.itemName == itemName)
{
if (_itemValueGreenSkin > 1)
{
PlayerPrefs.SetInt("ItemValueGreenSkin", 1);
CheckButtons();
return;
}
}
#endregion
#region GrayTrailUnEquip
if (item.itemName == itemName)
{
if (_itemValueGrayTrail > 1)
{
PlayerPrefs.SetInt("ItemValueGrayTrail", 1);
CheckButtons();
return;
}
}
#endregion
#region GreenTrailUnEquip
if (item.itemName == itemName)
{
if (_itemValueGreenTrail > 1)
{
PlayerPrefs.SetInt("ItemValueGreenTrail", 1);
CheckButtons();
return;
}
}
#endregion
#region RainbowTrailUnEquip
if (item.itemName == itemName)
{
if (_itemValueRainbowTrail > 0)
{
PlayerPrefs.SetInt("ItemValueRainbowTrail", 1);
CheckButtons();
return;
}
}
#endregion
}
}
public void Equip(string itemName)
{
_itemValueRainbowSkin = PlayerPrefs.GetInt("ItemValueRainbowSkin");
_itemValueGrayTrail = PlayerPrefs.GetInt("ItemValueGrayTrail");
_itemValueGreenTrail = PlayerPrefs.GetInt("ItemValueGreenTrail");
_itemValueGreenSkin = PlayerPrefs.GetInt("ItemValueGreenSkin");
_itemValueRainbowTrail = PlayerPrefs.GetInt("ItemValueRainbowTrail");
foreach (var item in shopItems)
{
#region RainbowSkinEquip
if (item.itemName == itemName)
{
if (_itemValueRainbowSkin > 0)
{
PlayerPrefs.SetInt("ItemValueRainbowSkin", 2);
if (_itemValueGreenSkin == 2)
{
PlayerPrefs.SetInt("ItemValueGreenSkin", 1);
}
CheckButtons();
return;
}
}
#endregion
#region RainbowTrailEquip
if (item.itemName == itemName)
{
if (_itemValueRainbowTrail > 0)
{
PlayerPrefs.SetInt("ItemValueRainbowTrail", 2);
if (_itemValueGreenTrail == 2)
{
PlayerPrefs.SetInt("ItemValueGreenTrail", 1);
}
if (_itemValueGrayTrail == 2)
{
PlayerPrefs.SetInt("ItemValueGrayTrail", 1);
}
CheckButtons();
return;
}
}
#endregion
#region GreenSkinEquip
if (item.itemName == itemName)
{
if (_itemValueGreenSkin > 0)
{
PlayerPrefs.SetInt("ItemValueGreenSkin", 2);
if (_itemValueRainbowSkin == 2)
{
PlayerPrefs.SetInt("ItemValueRainbowSkin", 1);
}
CheckButtons();
return;
}
}
#endregion
#region GrayTrailEquip
if (item.itemName == itemName)
{
if (_itemValueGrayTrail > 0)
{
PlayerPrefs.SetInt("ItemValueGrayTrail", 2);
if (_itemValueGreenTrail == 2)
{
PlayerPrefs.SetInt("ItemValueGreenTrail", 1);
}
if (_itemValueRainbowTrail == 2)
{
PlayerPrefs.SetInt("ItemValueRainbowTrail", 1);
}
CheckButtons();
return;
}
}
#endregion
#region GreenTrailEquip
if (item.itemName == itemName)
{
if (_itemValueGreenTrail > 0)
{
PlayerPrefs.SetInt("ItemValueGreenTrail", 2);
if (_itemValueGrayTrail == 2)
{
PlayerPrefs.SetInt("ItemValueGrayTrail", 1);
}
if (_itemValueRainbowTrail == 2)
{
PlayerPrefs.SetInt("ItemValueRainbowTrail", 1);
}
CheckButtons();
return;
}
}
#endregion
}
}
private void CheckButtons()
{
_itemValueRainbowSkin = PlayerPrefs.GetInt("ItemValueRainbowSkin");
_itemValueGrayTrail = PlayerPrefs.GetInt("ItemValueGrayTrail");
_itemValueGreenTrail = PlayerPrefs.GetInt("ItemValueGreenTrail");
_itemValueGreenSkin = PlayerPrefs.GetInt("ItemValueGreenSkin");
_itemValueRainbowTrail = PlayerPrefs.GetInt("ItemValueRainbowTrail");
_playerMoney = PlayerPrefs.GetFloat("money");
FindObjectOfType<ItemManager>().CheckForEquipedItems();
foreach (var item in shopItems)
{
if (item.itemName == "RainbowSkin")
{
switch (_itemValueRainbowSkin)
{
case 0:
if (!item.buyButton.activeSelf)
{
item.buyButton.SetActive(true);
}
if (item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(false);
}
if (item.equipButton.activeSelf)
{
item.equipButton.SetActive(false);
}
item.costText.text = "$" + item.cost;
break;
case 1:
if (item.buyButton.activeSelf)
{
item.buyButton.SetActive(false);
}
if (item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(false);
}
if (!item.equipButton.activeSelf)
{
item.equipButton.SetActive(true);
}
item.costText.text = "";
break;
case 2:
if (item.buyButton.activeSelf)
{
item.buyButton.SetActive(false);
}
if (!item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(true);
}
if (item.equipButton.activeSelf)
{
item.equipButton.SetActive(false);
}
item.costText.text = "";
break;
}
}
if (item.itemName == "GreenSkin")
{
switch (_itemValueGreenSkin)
{
case 0:
if (!item.buyButton.activeSelf)
{
item.buyButton.SetActive(true);
}
if (item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(false);
}
if (item.equipButton.activeSelf)
{
item.equipButton.SetActive(false);
}
item.costText.text = "$" + item.cost;
break;
case 1:
if (item.buyButton.activeSelf)
{
item.buyButton.SetActive(false);
}
if (item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(false);
}
if (!item.equipButton.activeSelf)
{
item.equipButton.SetActive(true);
}
item.costText.text = "";
break;
case 2:
if (item.buyButton.activeSelf)
{
item.buyButton.SetActive(false);
}
if (!item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(true);
}
if (item.equipButton.activeSelf)
{
item.equipButton.SetActive(false);
}
item.costText.text = "";
break;
}
}
if (item.itemName == "GrayTrail")
{
switch (_itemValueGrayTrail)
{
case 0:
if (!item.buyButton.activeSelf)
{
item.buyButton.SetActive(true);
}
if (item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(false);
}
if (item.equipButton.activeSelf)
{
item.equipButton.SetActive(false);
}
item.costText.text = "$" + item.cost;
break;
case 1:
if (item.buyButton.activeSelf)
{
item.buyButton.SetActive(false);
}
if (item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(false);
}
if (!item.equipButton.activeSelf)
{
item.equipButton.SetActive(true);
}
item.costText.text = "";
break;
case 2:
if (item.buyButton.activeSelf)
{
item.buyButton.SetActive(false);
}
if (!item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(true);
}
if (item.equipButton.activeSelf)
{
item.equipButton.SetActive(false);
}
item.costText.text = "";
break;
}
}
if (item.itemName == "GreenTrail")
{
switch (_itemValueGreenTrail)
{
case 0:
if (!item.buyButton.activeSelf)
{
item.buyButton.SetActive(true);
}
if (item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(false);
}
if (item.equipButton.activeSelf)
{
item.equipButton.SetActive(false);
}
item.costText.text = "$" + item.cost;
break;
case 1:
if (item.buyButton.activeSelf)
{
item.buyButton.SetActive(false);
}
if (item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(false);
}
if (!item.equipButton.activeSelf)
{
item.equipButton.SetActive(true);
}
item.costText.text = "";
break;
case 2:
if (item.buyButton.activeSelf)
{
item.buyButton.SetActive(false);
}
if (!item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(true);
}
if (item.equipButton.activeSelf)
{
item.equipButton.SetActive(false);
}
item.costText.text = "";
break;
}
}
if (item.itemName == "RainbowTrail")
{
switch (_itemValueRainbowTrail)
{
case 0:
if (!item.buyButton.activeSelf)
{
item.buyButton.SetActive(true);
}
if (item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(false);
}
if (item.equipButton.activeSelf)
{
item.equipButton.SetActive(false);
}
item.costText.text = "$" + item.cost;
break;
case 1:
if (item.buyButton.activeSelf)
{
item.buyButton.SetActive(false);
}
if (item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(false);
}
if (!item.equipButton.activeSelf)
{
item.equipButton.SetActive(true);
}
item.costText.text = "";
break;
case 2:
if (item.buyButton.activeSelf)
{
item.buyButton.SetActive(false);
}
if (!item.unequipButton.activeSelf)
{
item.unequipButton.SetActive(true);
}
if (item.equipButton.activeSelf)
{
item.equipButton.SetActive(false);
}
item.costText.text = "";
break;
}
}
}
}
public void BuySkinExecuter(string skinName)
{
BuySkinManager(skinName);
}
public void BuyTrailExecuter(string trailName)
{
BuyTrailManager(trailName);
}
private void BuySkinManager(string itemName)
{
_playerMoney = PlayerPrefs.GetFloat("money");
foreach (var item in shopItems)
{
switch (itemName)
{
case "RainbowSkin":
if (_playerMoney >= item.cost)
{
var moneytosaveafterpurchase = _playerMoney - item.cost;
PlayerPrefs.SetFloat("money", moneytosaveafterpurchase);
PlayerPrefs.SetInt("ItemValueRainbowSkin", 1);
CheckButtons();
if (SettingsManager.IsVibrationsOn)
{
if (MMVibrationManager.HapticsSupported())
{
MMVibrationManager.Haptic(HapticTypes.Success);
}
}
}
if (_playerMoney < item.cost)
{
NativeUI.AlertPopup.Alert("Alert!", "Not Enough Money!");
if (SettingsManager.IsVibrationsOn)
{
if (MMVibrationManager.HapticsSupported())
{
MMVibrationManager.Haptic(HapticTypes.Failure);
}
}
notEnoughText.Invoke();
}
break;
case "GreenSkin":
if (_playerMoney >= item.cost)
{
var moneytosaveafterpurchase = _playerMoney - item.cost;
PlayerPrefs.SetFloat("money", moneytosaveafterpurchase);
PlayerPrefs.SetInt("ItemValueGreenSkin", 1);
CheckButtons();
if (SettingsManager.IsVibrationsOn)
{
if (MMVibrationManager.HapticsSupported())
{
MMVibrationManager.Haptic(HapticTypes.Success);
}
}
}
if (_playerMoney < item.cost)
{
if (SettingsManager.IsVibrationsOn)
{
if (MMVibrationManager.HapticsSupported())
{
MMVibrationManager.Haptic(HapticTypes.Failure);
}
}
notEnoughText.Invoke();
}
break;
}
MoneyManager.Instance.UpdateMoneyText(MoneyManager.Instance.moneyCountenabled);
}
}
private void BuyTrailManager(string itemName)
{
_playerMoney = PlayerPrefs.GetFloat("money");
foreach (var item in shopItems)
{
switch (itemName)
{
case "GreenTrail":
if (_playerMoney >= item.cost)
{
var moneytosaveafterpurchase = _playerMoney - item.cost;
PlayerPrefs.SetFloat("money", moneytosaveafterpurchase);
PlayerPrefs.SetInt("ItemValueGreenTrail", 1);
CheckButtons();
if (SettingsManager.IsVibrationsOn)
{
if (MMVibrationManager.HapticsSupported())
{
MMVibrationManager.Haptic(HapticTypes.Success);
}
}
}
if (_playerMoney < item.cost)
{
if (SettingsManager.IsVibrationsOn)
{
if (MMVibrationManager.HapticsSupported())
{
MMVibrationManager.Haptic(HapticTypes.Failure);
}
}
notEnoughText.Invoke();
}
break;
case "GrayTrail":
if (_playerMoney >= item.cost)
{
var moneytosaveafterpurchase = _playerMoney - item.cost;
PlayerPrefs.SetFloat("money", moneytosaveafterpurchase);
PlayerPrefs.SetInt("ItemValueGrayTrail", 1);
CheckButtons();
if (SettingsManager.IsVibrationsOn)
{
if (MMVibrationManager.HapticsSupported())
{
MMVibrationManager.Haptic(HapticTypes.Success);
}
}
}
if (_playerMoney < item.cost)
{
if (SettingsManager.IsVibrationsOn)
{
if (MMVibrationManager.HapticsSupported())
{
MMVibrationManager.Haptic(HapticTypes.Failure);
}
}
notEnoughText.Invoke();
}
break;
case "RainbowTrail":
if (_playerMoney >= item.cost)
{
var moneytosaveafterpurchase = _playerMoney - item.cost;
PlayerPrefs.SetFloat("money", moneytosaveafterpurchase);
PlayerPrefs.SetInt("ItemValueRainbowTrail", 1);
CheckButtons();
if (SettingsManager.IsVibrationsOn)
{
if (MMVibrationManager.HapticsSupported())
{
MMVibrationManager.Haptic(HapticTypes.Success);
}
}
}
if (_playerMoney < item.cost)
{
if (SettingsManager.IsVibrationsOn)
{
if (MMVibrationManager.HapticsSupported())
{
MMVibrationManager.Haptic(HapticTypes.Failure);
}
}
notEnoughText.Invoke();
}
break;
}
MoneyManager.Instance.UpdateMoneyText(MoneyManager.Instance.moneyCountenabled);
}
}
}
just a hint you probably dont know because that class is almost 1000 lines long. There are very few cases where a class ever needs to be anywhere near that long and this is not one of them.
If you hit 300 lines doing unity dev then you probably doing something wrong.
Try breaking it into smaller components and the problem will no doubt reveal itself
It will also be easier to get help because I cant imagine many people that want to sit down and debug 1000 lines of someone else's code for free on the internet in their spare time.
This is a lot of code to look through, almost a thousand lines. If you could break it into pieces, figure out which part is causing the problem, and just post that piece, I think you would get better help.
Answer by rawa800x · Dec 31, 2020 at 11:46 AM
Is this about the unity asset store? If it is the store has moved to this link: https://assetstore.unity.com/
No it’s about a shop system I’m working on but it’s not working mainly the equip and unequip methods that’s don’t work and don’t know why so I’m trying to break up the coding to see if I could find the issue
Your answer
Follow this Question
Related Questions
How to make object visible without instantiating it? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
DateTime.now not working on iOS 0 Answers