using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemShop : MonoBehaviour
{
public static ItemShop itemShop;
public List<Items> itemList = new List<Items>();
public GameObject itemHolderPrefab;
public Transform grid;
void Start()
{
itemShop = this;
FillList();
}
void FillList()
{
for (int i = 0; i < itemList.Count; i++)
{
GameObject holder = Instantiate(itemHolderPrefab,grid);
ItemHolder holderScript = holder.GetComponent<ItemHolder>();
holderScript.itemName.text = itemList[i].itemName;
holderScript.itemPrice.text = itemList[i].weaponPrice.ToString("N2") + " Gems";
holderScript.itemID = itemList[i].itemID;
holderScript.buyButton.GetComponent<BuyButton>().itemID = itemList[i].itemID;
if (itemList[i].bought)
{
holderScript.itemImage.sprite = itemList[i].boughtSprite;
}
else
{
holderScript.itemImage.sprite = itemList[i].unboughtSprite;
}
}
}
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public GameObject GameOverPanel;
public GameObject LevelCompletePanel;
public TextMeshProUGUI currentScoreText;
public GameObject PauseButtons;
public GameObject Pausemenu;
public TextMeshProUGUI currentScoreText2;
float currentScore;
float currentScore2;
public string nextLevel = "2";
public int levelToUnlock = 2;
public static GameManager gameManager;
[SerializeField] private float money;
public TextMeshProUGUI moneyText;
void Start()
{
currentScore = 0;
SetScore();
currentScore2 = 0;
gameManager = this;
//money = 0;
//UpdateUI();
}
void Update()
{
//money = currentScore;
UpdateUI();
}
public void CallGameOver()
{
StartCoroutine(GameOver());
}
public void CallGameOver2()
{
StartCoroutine(GameOver2());
}
public void CallLevelComplete()
{
StartCoroutine(LevelComplete());
}
IEnumerator GameOver()
{
yield return new WaitForSeconds(1f);
GameOverPanel.SetActive(true);
PauseButtons.SetActive(false);
Destroy(Pausemenu);
yield break;
}
IEnumerator GameOver2()
{
yield return new WaitForSeconds(1f);
GameOverPanel.SetActive(true);
PauseButtons.SetActive(false);
Destroy(Pausemenu);
yield break;
}
IEnumerator LevelComplete()
{
yield return new WaitForSeconds(1f);
LevelCompletePanel.SetActive(true);
PauseButtons.SetActive(false);
Destroy(Pausemenu);
yield break;
}
public void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void Restart2()
{
SceneManager.LoadScene("SampleScene2Player");
}
public void Menu()
{
SceneManager.LoadScene("Start Menu");
}
public void GreyGem()
{
currentScore += 1f;
SetScore();
}
public void BlueGem()
{
currentScore += 5f;
SetScore();
}
public void GoldGem()
{
currentScore += 10f;
SetScore();
}
public void AddMoney(float amount)
{
money += amount;
UpdateUI();
}
public void ReduceMoney(float amount)
{
money -= amount;
UpdateUI();
}
public bool RequestMoney(float amount)
{
if(amount <= money)
{
return true;
}
return false;
}
void UpdateUI()
{
moneyText.text = "Gems: " + money.ToString("N2") ;
}
public void SetScore()
{
currentScoreText.text = currentScore.ToString();
currentScoreText2.text = currentScore2.ToString();
}
public void WinLevel()
{
Debug.Log("Level Completed");
PlayerPrefs.SetInt("levelReached", levelToUnlock);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuyButton : MonoBehaviour
{
public int itemID;
public void BuyItem()
{
if (itemID == 0)
{
Debug.Log("NO ITEM WITH THAT ID");
return;
}
for(int i = 0; i < ItemShop.itemShop.itemList.Count; i++)
{
if(ItemShop.itemShop.itemList[i].itemID == itemID && !ItemShop.itemShop.itemList[i].bought && GameManager.gameManager.RequestMoney(ItemShop.itemShop.itemList[i].weaponPrice))
{
ItemShop.itemShop.itemList[i].bought = true;
GameManager.gameManager.ReduceMoney(ItemShop.itemShop.itemList[i].weaponPrice);
return;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
[System.Serializable]
public class Items
{
public string itemName;
public int itemID;
public Sprite unboughtSprite;
public Sprite boughtSprite;
public float weaponPrice;
public bool bought;
}
public int itemID;
public TextMeshProUGUI itemName;
public TextMeshProUGUI itemPrice;
public Image itemImage;
public GameObject buyButton;
the problem it keeps bringing up anytime i click the buy button is this:
NullReferenceException: Object reference not set to an instance of an object
BuyButton.BuyItem () (at Assets/ShopSystem Script/BuyButton.cs:19)