To bring you up to speed, I’m creating a very basic shop outline for my game, now I’ve worked with game arrays before, but this is the first time I’m working with arrays that have multiple values, for instance, I have the following values applied per each actor available to buy :
[System.Serializable]
public class Character {
public GameObject actorPrefab;
public string actorLabel;
public int actorPrice;
public bool purchased = false;
}
Now, I have the full shop system working fine, the only thing missing now is the PlayerPrefs code for the purchased game objects.
When you purchase, the amount is taken from the coinsTotal ( this is saved in PlayerPrefs ), I then hide the BUY button and replace it with a SELECT button, if the player then presses SELECT, this becomes the default player model ( this is saved in PlayerPrefs ).
Now, when you purchase an actor, I set the actorPrice to 0 and boolean purchased to true, and I’m really unsure as to the best way to SAVE this information to the PlayerPrefs, or at least go about it, is there a special way to address an object from an array with multiple values or ? Would appreciate any advice going forward.
my buy Button function :
public void actorBuy() {
// Check we have enough coins to purchase
if (coinsTotal >= actors[index].actorPrice) {
// Then purchase actor
coinsTotal = coinsTotal - actors[index].actorPrice; // Reduce coinsTotal by actorPrice ( save this to PlayerPrefs )
textMoney.text = "Coins : " + coinsTotal.ToString(); // Update our coin amount display
actors[index].purchased = true; // Set purchased to be true ( save this to PlayerPrefs )
actors[index].actorPrice = 0; // Set actorPrice to be zero ( save this to PlayerPrefs )
// Update screen ui elements
actorPurchased();
textReferences();
}
if (coinsTotal < actors[index].actorPrice) {
Debug.Log ("Not Enough Coins!");
// Then do something else here
}
}