Hi All,
So my store for my game is all done except that the price text on an item doesn’t save its state (enabled/disabled) after the game has been closed and re-opened. For example, when you buy an item, the price text of the item is disabled (this part is working), and then after you close and re-open the game the price text should still be disabled (this is where it doesn’t work, the text is re-enabled). I’m not to sure if I’m saving the text’s state properly in order for this to work.
Script for the store and disabling the text:
{
public AudioSource[] sounds;
public AudioSource source1;
public AudioSource source2;
public AudioSource source3;
private int price; //cost of the models
public List<GameObject> models;
public int selectionIndex = 0; //Default index of the models
private int[] prices = { 0, 100, 200}; //prices. Set for each model
public Text[] pricesText; //text
private void Start ()
{
sounds = GetComponents<AudioSource> ();
source1 = sounds [0]; //player has selected a previoulsy unlocked item
source2 = sounds [1]; //player can't afford an item
source3 = sounds [2]; //player unlocked an item
selectionIndex = PlayerPrefs.GetInt ("CharacterSelected");
models = new List<GameObject> ();
foreach(Transform t in transform)
{
models.Add (t.gameObject);
t.gameObject.SetActive (false);
}
models [selectionIndex].SetActive (true);
}
public void OnClick (int index)
{
Text priceText = pricesText[index]; //text
//Select unlocked characters
if ((DataManagement.datamanagement.modelAvailibilty & 1 << index) == 1 << index)
{
models [selectionIndex].SetActive (false);
selectionIndex = index;
models [selectionIndex].SetActive (true);
source2.Play (); //previously unlocked item has been selected
priceText.enabled = false; //disables text
if (index == selectionIndex)
{
PlayerPrefs.SetInt ("CharacterSelected", index);
}
}
else
{
//Unlock an item
int price = prices[index];
Debug.Log (price);
if (DataManagement.datamanagement.totalCoins >= price)
{
DataManagement.datamanagement.totalCoins -= price;
DataManagement.datamanagement.modelAvailibilty += 1 << index;
models [selectionIndex].SetActive (false);
selectionIndex = index;
models [selectionIndex].SetActive (true);
PlayerPrefs.SetInt ("CharacterSelected", index);
source3.Play (); //player has unlocked an item
priceText.enabled = false; //disables text
}
else
{
//Player doesnt have enough coins to unlock the item
source1.Play ();
}
}
DataManagement.datamanagement.SaveData ();
}
}
DataMangement:
{
public static DataManagement datamanagement;
[Space(10)]
public int currentCoins;
public int totalCoins; //Sets how much the player will have the first time they play
[Space(10)]
public int currentDistance;
public int totalDistance;
[Space(10)]
public int highScore;
[Space(10)]
public int modelAvailibilty = 1;
private Text[] pricesText; //text
void Awake ()
{
if (PlayerPrefs.HasKey ("CharacterSelected"))
{
modelAvailibilty = PlayerPrefs.GetInt ("ModelAvailibilty");
}
if (datamanagement == null)
{
DontDestroyOnLoad (gameObject);
datamanagement = this;
}
else if (datamanagement != this)
{
Destroy (gameObject);
}
}
public void SaveData ()
{
BinaryFormatter binForm = new BinaryFormatter();
FileStream file = File.Create (Application.persistentDataPath + "/gameInfo.dat");
gameData data = new gameData();
data.highScore = highScore;
data.totalCoins = totalCoins;
data.totalDistance = totalDistance;
data.modelAvailibilty = modelAvailibilty;
data.pricesText = pricesText; //text
binForm.Serialize (file, data);
file.Close ();
}
public void LoadData ()
{
if (File.Exists (Application.persistentDataPath + "/gameInfo.dat"))
{
BinaryFormatter binForm = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/gameInfo.dat", FileMode.Open);
gameData data = (gameData)binForm.Deserialize (file);
file.Close ();
highScore = data.highScore;
totalCoins = data.totalCoins;
totalDistance = data.totalDistance;
modelAvailibilty = data.modelAvailibilty;
pricesText = data.pricesText; //text
}
}
}
[Serializable]
class gameData
{
public int highScore;
public int totalCoins;
public int totalDistance;
public int modelAvailibilty;
public Text[] pricesText; //text
}
This is my first game and I am very new to programming so any advice/help is greatly appreciated!! ![]()






