Hey guys!
I’m relativley new to Unity and I have a problem with the saving of an unlocked character! So basically I have a shop scene where you can unlock new characters with your coins/money, but when you unlock/buy something (which works fine) and then you stop the inspector and start again you would have to buy this character again, because I don’t know how to save it!
Here are my scripts and I think that I should add a line in the “BuysScript2” in line 35, because that’s where I unlock the character!
Thanks for your help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class globalcurrency {
public static int character;
public static bool character2unlocked;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BuyScript : MonoBehaviour {
public int myChar;
private float price;
public Text coinss;
public Text pricetext;
void Start() {
price = GetComponent<BuyScript2> ().price;
}
void Update(){
if (myChar == 1)
{
if (globalcurrency.character == 1)
{
pricetext.text = "selected";
}
else
{
pricetext.text = "select";
}
}
if (myChar == 2)
{
if (globalcurrency.character2unlocked)
{
if (globalcurrency.character == 2)
{
pricetext.text = "selected";
}
else
{
pricetext.text = "select";
}
}
else
{
pricetext.text = price + "x" ;
}
}
coinss.text = ((int)PlayerPrefs.GetInt ("Coins")).ToString () + "x";
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuyScript2 : MonoBehaviour {
private int myChar;
public int price;
private int money;
// Use this for initialization
void Awake () {
myChar = GetComponent<BuyScript> ().myChar;
money = PlayerPrefs.GetInt ("Coins");
}
// Update is called once per frame
void Update () {
if (myChar == 1) {
globalcurrency.character = 1;
}
if (myChar == 2) {
if (globalcurrency.character2unlocked) {
globalcurrency.character = 2;
} else {
if (money >= price) {
PlayerPrefs.SetInt("Coins", money - price);
PlayerPrefs.Save();
globalcurrency.character2unlocked = true;
}
}
}
GetComponent<BuyScript2> ().enabled = false;
}
}