I am trying to create a shop system in my game with the help of ChatGPT. I have made prefabs of my character skins, but the problem is that after changing scenes, the characters are not activated. Specifically, I have set all characters to be inactive by default, with only the default character being active. However, after changing scenes, no characters are active.
Here is my code:
<using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class Character : MonoBehaviour
{
public static Character instance; // Static instance for easy access
public List<GameObject> characters; // List of character GameObjects
public List<int> characterCosts; // List of character costs
public List<Button> characterSelectButtons; // List of buttons for selecting characters
public List<Button> characterPurchaseButtons; // List of buttons for purchasing characters
public List<Image> characterSelectionIndicators; // List of images to indicate selected characters
public TMP_Text coinsText; // Text for displaying coins
private int selectedCharacterIndex = 0; // Index of the selected character
private const int defaultCharacterIndex = 0; // Index for the default character
private const string SelectedCharacterKey = "SelectedCharacter";
private const string PurchasedCharactersKey = "PurchasedCharacters";
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
// Ensure the default character is always unlocked
PlayerPrefs.SetInt(PurchasedCharactersKey + defaultCharacterIndex, 1);
LoadCharacterSelection();
UpdateCharacterSelection();
UpdateAllButtons();
UpdateCoinsText();
}
public void PurchaseCharacter(int characterIndex)
{
// Skip purchase logic for the default character
if (characterIndex == defaultCharacterIndex)
{
Debug.Log("Default character cannot be purchased.");
return;
}
int currentCoins = CoinManager.instance.GetCoins();
if (IsCharacterPurchased(characterIndex))
{
Debug.Log("Character already purchased!");
return;
}
if (currentCoins >= characterCosts[characterIndex])
{
// Reduce the player's coins by the cost of the character
CoinManager.instance.SpendCoins(characterCosts[characterIndex]);
// Unlock the purchased character
UnlockCharacter(characterIndex);
// Save the purchase and update UI elements
SaveCharacterPurchase(characterIndex);
UpdateAllButtons();
UpdateCoinsText();
Debug.Log("Character purchased!");
}
else
{
Debug.Log("Not enough coins!");
}
}
public void SelectCharacter(int characterIndex)
{
if (IsCharacterPurchased(characterIndex) || characterIndex == defaultCharacterIndex) // Allow selection if purchased or default character
{
selectedCharacterIndex = characterIndex;
SaveCharacterSelection(characterIndex);
UpdateCharacterSelection();
UpdateSelectionIndicators();
}
else
{
Debug.Log("Character not purchased yet!");
}
}
private void UpdateCharacterSelection()
{
for (int i = 0; i < characters.Count; i++)
{
characters[i].SetActive(i == selectedCharacterIndex);
}
UpdateSelectionIndicators();
}
private void UpdateSelectionIndicators()
{
for (int i = 0; i < characterSelectionIndicators.Count; i++)
{
characterSelectionIndicators[i].enabled = (i == selectedCharacterIndex);
}
}
private void UpdateAllButtons()
{
for (int i = 0; i < characters.Count; i++)
{
bool purchased = IsCharacterPurchased(i);
characterSelectButtons[i].interactable = purchased;
characterPurchaseButtons[i].gameObject.SetActive(!purchased && i != defaultCharacterIndex);
}
}
private bool IsCharacterPurchased(int characterIndex)
{
return PlayerPrefs.GetInt(PurchasedCharactersKey + characterIndex, 0) == 1;
}
private void SaveCharacterPurchase(int characterIndex)
{
PlayerPrefs.SetInt(PurchasedCharactersKey + characterIndex, 1);
PlayerPrefs.Save();
}
private void SaveCharacterSelection(int characterIndex)
{
PlayerPrefs.SetInt(SelectedCharacterKey, characterIndex);
PlayerPrefs.Save();
}
private void LoadCharacterSelection()
{
selectedCharacterIndex = PlayerPrefs.GetInt(SelectedCharacterKey, defaultCharacterIndex);
}
private void UnlockCharacter(int characterIndex)
{
// Additional logic for unlocking a character if needed
}
public void UpdateCoinsText()
{
coinsText.text = $"Coins: {CoinManager.instance.GetCoins()}";
}
}>
Any solution???