PlayerPrefs. Load Selected Character from previous scene
Hi, so I've encountered a problem with my character selection.
To explain what I want to add: In main menu you can choose the character you want to play with. You click select. Then you can start the game with the character you selected in main menu, in previous scene.
What is happening: In main menu, you choose and click select the character, then you press play, another scene loads, but you play with a character you didn't select. The default character loads. Simply the PlayerPrefs isn't working.
I have my 8 characters stored in a parent GameObject. They are activating and deactivating as you choose them in main menu. The Same Script is also in another scene in the parent GameObject. So, how should I load the index character from one scene to another? I'm starting to get desperate and out of ideas. I have the same spcript on the same prefab "player" as you can see on the picture. But the index isn't choosen from the previous scene and "char1" is only active.
The Script for Character Selection:
[code=CSharp]
public GameObject[] characterList;
public int index;
public SmoothCamera2D myCam;
public 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 Left()
{
characterList[index].SetActive(false);
index--;
if (index < 0)
index = characterList.Length - 1;
characterList[index].SetActive(true);
myCam.target = characterList[index].transform;
}
public void Right()
{
characterList[index].SetActive(false);
index++;
if (index == characterList.Length)
index = 0;
characterList[index].SetActive(true);
myCam.target = characterList[index].transform;
}
public void SelectCharacter()
{
PlayerPrefs.SetInt("CharacterSelected", index);
myCam.target = characterList[index].transform;
}
[/code]
[1]: /storage/temp/192399-bez-nazvu.png
please share both your menu and game functions witch are responsible for this and also use code tag to send cause right now I cant read it
Edited the script so it's easier to read it. My bad, i'm sorry. The Last void SelectCharacter() is what i have for selecting the player in UI button. This script is on prefab "player" which I have the same as in menu so in the game. I thought the void Start() will do the thing in the game to get the index of the character and set it active.
There is nothing wrong in this code , u did every thing on point .. when these kinds of problems happens your best shot is to print every thing and see witch data is not right ... like print the index value before saving it and print the value when loading in start function etc ...
Answer by xxmariofer · Feb 11 at 11:20 AM
this line makes no sense:
if (characterList[index])
characterList[index].SetActive(true);
what are you trying to do? simply remove the if?
Your answer
Follow this Question
Related Questions
How do I save a previously bought characters in my Game? 0 Answers
Sharing or importing Save Data between games 0 Answers
Need Help with High Score Please. 0 Answers
Problem saving with PlayerPrefs 0 Answers