Hi a I wrote some scripts to create a sort of shop but they didn’t work. I would that the hats and then when I buy them,in another menu they are activated by the purchase.In a second time aI would switch off from hat to hat
Someone could help ?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Buyer : MonoBehaviour
{
public int price = 0;
private Text coinsText;
public int coinCount = 0;
private CoinCounter coinCounter;
void Start ()
{
coinCounter = GameObject.Find("CoinCount").GetComponent<CoinCounter>();
if(!PlayerPrefs.HasKey("coins")) // if the key does not exist in the PlayerPrefs file
{
PlayerPrefs.SetInt("coins", coinCounter.coinCount); // create it and set it to the value of coinCounter.coinCount
}
coinCounter.coinCount = PlayerPrefs.GetInt("coins"); // get the PlayerPrefs coins amount
// check to see if the PlayerPrefs file has stored any hats
if(!PlayerPregs.HasKey("Cappello1")) // if this key does not exist in the PlayerPrefs file
{
PlayerPrefs.SetInt("Cappello1", 0); // create the key and set it's value to 0 (false)
}
if(!PlayerPregs.HasKey("Cappello2")) // if this key does not exist in the PlayerPrefs file
{
PlayerPrefs.SetInt("Cappello2", 0); // create the key and set it's value to 0 (false)
}
if(!PlayerPregs.HasKey("Cappello3")) // if this key does not exist in the PlayerPrefs file
{
PlayerPrefs.SetInt("Cappello3", 0); // create the key and set it's value to 0 (false)
}
if(!PlayerPregs.HasKey("Cappello4")) // if this key does not exist in the PlayerPrefs file
{
PlayerPrefs.SetInt("Cappello4", 0); // create the key and set it's value to 0 (false)
}
PlayerPrefs.Save(); // save the changes to the PlayerPrefs file
}
public void Buy()
{
if (coinCounter.coinCount < price) // if the user doesn't have enough coins
{
return; // don't do anything.
}
if (coinCounter.coinCount >= price) // if the user has enough coins
{
if(this.name == "Hat1" && PlayerPrefs.GetInt("Cappello1") == 0) // if this is the Hat1 button & the player doesn't already own Hat #1
{
coinCounter.coinCount -= price; // take away the cost of this item
PlayerPrefs.SetInt("Cappello1", 1); // make it so the player has access to Hat #1
}
if(this.name == "Hat2" && PlayerPrefs.GetInt("Cappello2") == 0) // if this is the Hat2 button & the player doesn't already own Hat #2
{
coinCounter.coinCount -= price; // take away the cost of this item
PlayerPrefs.SetInt("Cappello2", 1); // make it so the player has access to Hat #2
}
if(this.name == "Hat3" && PlayerPrefs.GetInt("Cappello3") == 0) // if this is the Hat3 button & the player doesn't already own Hat #3
{
coinCounter.coinCount -= price; // take away the cost of this item
PlayerPrefs.SetInt("Cappello3", 1); // make it so the player has access to Hat #3
}
if(this.name == "Hat4" && PlayerPrefs.GetInt("Cappello4") == 0) // if this is the Hat4 button & the player doesn't already own Hat #4
{
coinCounter.coinCount -= price; // take away the cost of this item
PlayerPrefs.SetInt("Cappello4", 1); // make it so the player has access to Hat #4
}
coinsText.text = coinCounter.coinCount.ToString(); // change the coinsText to be the user's current coin amount
PlayerPrefs.SetInt("coins", coinCounter.coinCount); // put the current coin amount in the PlayerPrefs file
PlayerPrefs.Save(); // and finally, save the PlayerPrefs file.
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ChooseHat : MonoBehaviour {
public GameObject hat1;
public GameObject hat2;
public GameObject hat3;
public GameObject hat4;
void Start()
{
// check to see if the PlayerPrefs file has stored any hats
if(!PlayerPregs.HasKey("Cappello1")) // if this key does not exist in the PlayerPrefs file
{
PlayerPrefs.SetInt("Cappello1", 0); // create the key and set it's value to 0 (false)
}
if(!PlayerPregs.HasKey("Cappello2")) // if this key does not exist in the PlayerPrefs file
{
PlayerPrefs.SetInt("Cappello2", 0); // create the key and set it's value to 0 (false)
}
if(!PlayerPregs.HasKey("Cappello3")) // if this key does not exist in the PlayerPrefs file
{
PlayerPrefs.SetInt("Cappello3", 0); // create the key and set it's value to 0 (false)
}
if(!PlayerPregs.HasKey("Cappello4")) // if this key does not exist in the PlayerPrefs file
{
PlayerPrefs.SetInt("Cappello4", 0); // create the key and set it's value to 0 (false)
}
PlayerPrefs.Save(); // save the changes to the PlayerPrefs file
}
public void Hat1 () {
hat1 = PlayerPrefs.GetInt("Cappelo1"); // check the value stored in the PlayerPrefs file
if(hat1 == 1) // if the player owns hat 1
{
hat1.SetActive(true); // it can be active
hat2.SetActive(false);
hat3.SetActive(false);
hat4.SetActive(false);
}
if(hat1 == 0) // if the player does not own hat 1
{
hat1.SetActive(false); // make it inactive
hat2.SetActive(false);
hat3.SetActive(false);
hat4.SetActive(false);
}
}
public void Hat2 () {
hat2 = PlayerPrefs.GetInt("Cappelo2"); // check the value stored in the PlayerPrefs file
if(hat2 == 1) // if the player owns hat 2
{
hat1.SetActive(false); // it can be active
hat2.SetActive(true);
hat3.SetActive(false);
hat4.SetActive(false);
}
if(hat2 == 0) // if the player does not own hat 2
{
hat1.SetActive(false); // make it inactive
hat2.SetActive(false);
hat3.SetActive(false);
hat4.SetActive(false);
}
}
public void Hat3 () {
hat3 = PlayerPrefs.GetInt("Cappelo3"); // check the value stored in the PlayerPrefs file
if(hat3 == 1) // if the player owns hat 3
{
hat1.SetActive(false); // it can be active
hat2.SetActive(false);
hat3.SetActive(true);
hat4.SetActive(false);
}
if(hat3 == 0) // if the player does not own hat 3
{
hat1.SetActive(false); // make it inactive
hat2.SetActive(false);
hat3.SetActive(false);
hat4.SetActive(false);
}
}
public void Hat4 () {
hat4 = PlayerPrefs.GetInt("Cappelo4"); // check the value stored in the PlayerPrefs file
if(hat4 == 1) // if the player owns hat 4
{
hat1.SetActive(false); // it can be active
hat2.SetActive(false);
hat3.SetActive(false);
hat4.SetActive(true);
}
if(hat4 == 0) // if the player does not own hat 4
{
hat1.SetActive(false); // make it inactive
hat2.SetActive(false);
hat3.SetActive(false);
hat4.SetActive(false);
}
}
}