using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class CharactersScript : MonoBehaviour { public GameObject[] characters; // Array of character models or portraits private int currentIndex = 0; public Button nextButton; public Button prevButton; public Button selectButton; // Separate "Select" button for current character void Start() { nextButton.onClick.AddListener(ShowNextCharacter); prevButton.onClick.AddListener(ShowPreviousCharacter); selectButton.onClick.AddListener(OnSelectButtonClick); // Load the saved character index from PlayerPrefs if (PlayerPrefs.HasKey("selectedCharacterIndex")) { currentIndex = PlayerPrefs.GetInt("selectedCharacterIndex"); } else { currentIndex = 0; // Default to the first character PlayerPrefs.SetInt("selectedCharacterIndex", currentIndex); PlayerPrefs.Save(); } // Validate the loaded index if (currentIndex < 0 || currentIndex >= characters.Length) { currentIndex = 0; // Default to the first character if the saved index is invalid } // Show the initial character and update the button text ShowCharacter(currentIndex); UpdateSelectButtonText(currentIndex); } void ShowNextCharacter() { currentIndex = (currentIndex + 1) % characters.Length; ShowCharacter(currentIndex); UpdateSelectButtonText(currentIndex); } void ShowPreviousCharacter() { currentIndex = (currentIndex - 1 + characters.Length) % characters.Length; ShowCharacter(currentIndex); UpdateSelectButtonText(currentIndex); } void OnSelectButtonClick() { // Save the selected character index to PlayerPrefs PlayerPrefs.SetInt("selectedCharacterIndex", currentIndex); PlayerPrefs.Save(); Debug.Log("Current Index: " + currentIndex); Debug.Log("Selected Character: " + characters[currentIndex].name); // Update the "Select" button text to "Selected" UpdateSelectButtonText(currentIndex); } void ShowCharacter(int index) { // Hide all characters foreach (GameObject character in characters) { character.SetActive(false); } // Show the character at the specified index if (index >= 0 && index < characters.Length) { characters[index].SetActive(true); } } void UpdateSelectButtonText(int index) { // Update the "Select" button text to "Selected" if this character is the one saved in PlayerPrefs int savedIndex = PlayerPrefs.GetInt("selectedCharacterIndex", -1); if (index == savedIndex) { selectButton.GetComponentInChildren().text = "Selected"; } else { selectButton.GetComponentInChildren().text = "Select"; } } public void playGame() { SceneManager.LoadScene("Level_1 New Enemies"); } }