How can I save my character selection? I write these lines but when I reopen the game or reload the scene I lost my selection and selectedCharacter equals 0. For example if I choose second character, selectedCharacter should equal the second character value and when I reload the scene selectedCharacter should equal the second character value.
I will be so happy if you help me.
using UnityEngine;
using UnityEngine.SceneManagement;
public class Characters : MonoBehaviour
{
public GameObject[] characters;
public int selectedCharacter=0;
void Start() {
while(0 < PlayerPrefs.GetInt("selectedCharacter"))
NextCharacter();
}
public void NextCharacter()
{
characters[selectedCharacter].SetActive(false);
selectedCharacter = (selectedCharacter + 1) % characters.Length;
characters[selectedCharacter].SetActive(true);
PlayerPrefs.SetInt("selectedCharacter", selectedCharacter);
}
public void PreviousCharacter()
{
characters[selectedCharacter].SetActive(false);
selectedCharacter--;
if (selectedCharacter < 0)
{
selectedCharacter += characters.Length;
}
characters[selectedCharacter].SetActive(true);
PlayerPrefs.SetInt("selectedCharacter", selectedCharacter);
}
public void StartGame()
{
PlayerPrefs.SetInt("selectedCharacter", selectedCharacter);
SceneManager.LoadScene(1,LoadSceneMode.Single);
}
}
![alt text][1]
[1]: /storage/temp/185249-a.png
↧