- Home /
About playerprefs, character selector and shop
Hello guys, I want to build a shop and i have created a character selection mode until now. So basicly i have a script that looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CharacterSelection : MonoBehaviour {
private GameObject[] characterList;
private int index;
public string lvltoload = "MainLevel";
private void Start()
{
index = PlayerPrefs.GetInt("CharacterSelected");
characterList = new GameObject[transform.childCount];
for (int i = 0; i < transform.childCount; i++)
{
characterList[i] = transform.GetChild(i).gameObject;
}
foreach (GameObject go in characterList)
{
go.SetActive(false);
}
if (characterList[index])
{
characterList[index].SetActive(true);
}
}
public void ToggleLeft()
{
characterList[index].SetActive(false);
index -= 1;
if(index < 0)
{
index = characterList.Length - 1;
}
characterList[index].SetActive(true);
}
public void ToggleRight()
{
characterList[index].SetActive(false);
index += 1;
if (index == characterList.Length)
{
index = 0;
}
characterList[index].SetActive(true);
}
public void ConfirmButton()
{
PlayerPrefs.SetInt("CharacterSelected", index);
SceneManager.LoadScene(lvltoload);
}
}
And i want the user to firstly buy the character and after that to select it. But i don't know how to do it. I was thinking to set a playerprefs but i don't know how to set it (the script) for each index. For example if index 1(character 1) has the PlayerPrefs ("CharacterSold", 0) then the button select to be inactive. And now the player should buy the character. So the PlayerPrefs becomes 1 and now the select button is active. Can you help me with that?
Answer by FlaSh-G · Nov 07, 2017 at 10:45 AM
So basically your question is "how do I save an arbitrary amount of values into PlayerPrefs"?
There's multiple ways to do that. Especially since your values are simple booleans. One could be a string with lots of ones and zeroes:
// Load
var characterUnlocks = PlayerPrefs.GetString("CharacterUnlocks");
for(var i = 0; i < characterUnlocks.Length; i++)
{
// Doesn't work, but it shows the idea
characterList[i].unlocked = characterUnlocks[i] == '1';
}
// Save
var sb = new StringBuilder();
for(var i = 0; i < characterList.Length; i++)
{
sb.Append(characterList[i].unlocked ? '1' : '0');
}
PlayerPrefs.SetString("CharacterUnlocks", sb.ToString());
Of course, there is no such thing as GameObject.unlocked, you'll have to write something there. Probably reference some sort of character script in your array rather than the GameObjects.
Another way would be to store multiple PlayerPrefs data points:
// Load
for(var i = 0; i < characterList.Length; i++)
{
characterList[i].unlocked = PlayerPrefs.GetInt("UnlockedCharacter" + i, 0) == 1;
}
// Save
for(var i = 0; i < characterList.Length; i++)
{
PlayerPrefs.SetInt("UnlockedCharacter" + i, characterList[i].unlocked ? 1 : 0);
}
Note the PlayerPrefs key string based on the value of i.
Answer by madhu_kumar · Nov 07, 2017 at 12:50 PM
for(var i = 0; i < characterList.Length; i++) { if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 0) // { //not buyed } if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 1) // { //buyed but not selected } if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 2) // { //selected } }
for(var i = 0; i < characterList.Length; i++) { if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 0) { //not buyed } if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 1) { //buyed but not selected } if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 2) { //selected } }