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][1] and [Simple Shop][2].
I combined the two videos, what I came up with is a character select screen similar to the first video with a buy button[164742-character-select.jpg*|164742]
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 *= 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
[1]: Character Selection (And changing scene) - Unity 3D[Tutorial][C#] - YouTube
[2]: How to make a shop or item store in 2D Unity game. Simple Unity 2D tutorial. - YouTube
*