- Home /
Unity Character Selection + store
Hi! I've been having trouble making a character selection screen for my game. My goal is to make a character selection scene where you could purchase characters with coins that you collect from the game. I created the scripts from what I've learned from these two videos: Character Select Scene and Simple Shop.
I combined the two videos, what I came up with is a character select screen similar to the first video with a buy button
All the character models are attached to an empty game object called player models with this script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CharacterScript : MonoBehaviour
{
private GameObject[] characterList;
private int index;
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--;
if (index < 0)
index = characterList.Length - 1;
characterList[index].SetActive(true);
}
public void ToggleRight()
{
characterList[index].SetActive(false);
index++;
if (index == characterList.Length)
index = 0;
characterList[index].SetActive(true);
}
public void ConfirmButton()
{
PlayerPrefs.SetInt("CharacterSelected", index);
SceneManager.LoadScene(1);
}
public void MainMenuButton()
{
SceneManager.LoadScene(0);
}
}
It basically controls the arrow buttons and the confirm button which works perfectly. The issue is the buying part.
I currently have 37 models. What I've done is create duplicates for the buy button and the confirm button and placed them as a child for each character model (So basically there are 37 buy buttons and confirm buttons) and I also have multiple copies of this script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class f_char02 : MonoBehaviour
{
int coinAmount1;
int isBought1;
public Text coinAmountText1;
public Button confirmButton1;
public Button buyButton1;
void Start()
{
coinAmount1 = PlayerPrefs.GetInt("Coins");
confirmButton1.gameObject.SetActive(true);
buyButton1.gameObject.SetActive(true);
}
private void Update()
{
coinAmountText1.text = "x " + coinAmount1.ToString();
isBought1 = PlayerPrefs.GetInt("IsBought");
if (coinAmount1 >= 50 && isBought1 == 0)
{
buyButton1.interactable = true;
confirmButton1.interactable = false;
}
else if (coinAmount1 >= 50 && isBought1 == 1)
{
buyButton1.interactable = false;
confirmButton1.interactable = true;
}
else if (coinAmount1 <= 50 && isBought1 == 0)
{
confirmButton1.interactable = false;
buyButton1.interactable = false;
}
else if (coinAmount1 <= 50 && isBought1 == 1)
{
confirmButton1.interactable = true;
buyButton1.interactable = false;
}
}
public void BuyButton()
{
coinAmount1 -= 50;
PlayerPrefs.SetInt("IsBought", 1);
buyButton1.gameObject.SetActive(false);
}
}
which is in each of the character models. When I press buy on one model, it purchases every single other character in the game even though I only bought the first one which does not make sense given the fact that I duplicated every single button there is. Another problem is that my gold is reduced when hovering over the character I bought but when I switch to a different character my gold is back where it started. I am open to remaking this character select menu and I will accept all suggestions
Sorry for the long post I just wanted to make this as detailed as possible, thanks in advance
Answer by davidcox70 · Aug 04, 2020 at 02:17 PM
This is because you only have a single saved user setting called "IsBought". You said that this same code is on all your purchase options, so which ever one is purchased will set IsBought=1 and save it as a user preference. This single setting is then referenced by every single model, which is what is making them appear as purchased. So you will need to have a different users prefs variable for each model.
Yeah, I remade the code yesterday, I gave the variables different names like: isbought1 isbought2 and so on but some issues still persist like my money resetting back to what it used to be before I bought the item when I switch scenes. But Giving them different names solved one part of the problem though. Thanks
Good to know. Please accept the answer to round off the question. Regarding money resetting, a good practice when using playerPrefs is to enforce saving by adding a PlayerPrefs.Save(); when you've set some variables. This will make sure the data is saved to the devices storage as otherwise it gets saved at a later time, which might not be in time for your next reading of that data.
Hey I found a new issue. What happened is I collected 500 coins then I spent it on one character and my coins are reduced by 50 which is what is supposed to happen, then I switch to another model to attempt to buy it but my coins went back up to 500