- Home /
Shop system?
I am using player.prefs to everything BUT now I want to make some var wchich will know if it is item bought. 0 will say yes and 1 will say no.
if(PlayerPrefs.GetInt("P2b") == 0)
This is in function update (below this code is if player clicked on item)
Unity says that he dont know P2b so I make this
PlayerPrefs.SetInt("P2b", P2b);
This isnt in any function (P2b is var int and it is 0)
But the shop waorks like this : u something buy it will subtract your money it will show tick icon. but if u close the scene every thing will ruined up U dont have to read this but this is my all script private var ray : Ray; private var rayCastHit : RaycastHit; var P2b : int = 0; var P3b : int = 0; var P4b : int = 0; var B : int = 1; var M3T : TextMesh; var t2 : GameObject; var t3 : GameObject; var t4 : GameObject;
PlayerPrefs.SetInt("P2b", P2b);
PlayerPrefs.SetInt("P3b", P3b);
PlayerPrefs.SetInt("P4b", P4b);
function Update ()
{
M3T.text = " " + PlayerPrefs.GetInt("MoneyP");
Debug.Log(PlayerPrefs.GetInt("MoneyP"));
if (Input.GetKeyDown(KeyCode.Escape)) { Application.LoadLevel("Menu"); }
if(Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, rayCastHit))
{
if(rayCastHit.transform.name == "P2")
{
if(PlayerPrefs.GetInt("P2b") == 0)
{
if(PlayerPrefs.GetInt("MoneyP")>2000)
{
P2bought();
} }}
if(rayCastHit.transform.name == "P3")
{
if(PlayerPrefs.GetInt("P3b") == 0)
{
if(PlayerPrefs.GetInt("MoneyP")>5000)
{
P3bought();
} }}
if(rayCastHit.transform.name == "P4")
{
if(PlayerPrefs.GetInt("P4b") == 0)
{
if(PlayerPrefs.GetInt("MoneyP")>10000)
{
P4bought();
} }}
}}}
function P2bought()
{
PlayerPrefs.SetInt("MoneyP", PlayerPrefs.GetInt("MoneyP") - 2000);
PlayerPrefs.SetInt("P2b",B );
t2.SetActive(true);
}
function P3bought()
{
PlayerPrefs.SetInt("MoneyP", PlayerPrefs.GetInt("MoneyP") - 5000);
PlayerPrefs.SetInt("P3b",B );
t3.SetActive(true);
}
function P4bought()
{
PlayerPrefs.SetInt("MoneyP", PlayerPrefs.GetInt("MoneyP") - 10000);
PlayerPrefs.SetInt("P4b",B );
t4.SetActive(true);
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613125710im_/https://answers.unity.com/themes/thub/images/avi.jpg)