Help with saving a charcater selection in unity.

Updated on November 13, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on November 12, 2018

I am working on a character selection menu and when the player buys a new charcter and dies playing that character the scene gets reloaded. And when the scene gets reloaded the charcter goes back to the original one. I am wondering if there is a way to save that charcter selection even when the scene gets reloaded.

Here is the code that runs when the player buys a new character.

 

 public void YellowPlaneSelect()
 {
 if (TheScore.thePoints >= yellowPlaneCost && yellowPlaneSave == 0)
 {
 Destroy(currentPlayer);
 GameObject newPlayer = Instantiate(yellowPlane);
 yellowPlaneSave = 1;
 PlayerPrefs.SetInt("yellowPlaneSave", yellowPlaneSave);
 PlayerPrefs.SetString("yellowPlaneText", Select);
 TheScore.thePoints -= yellowPlaneCost;
 PlayerPrefs.SetInt("Money", TheScore.thePoints);
 newPlayer.GetComponent<movemnt>().enabled = false;
 newPlayer = currentPlayer;
 characterSelect.SetActive(false);
 }
 else if (yellowPlaneSave == 1)
 {
 Destroy(currentPlayer);
 GameObject newPlayer = Instantiate(yellowPlane);
 newPlayer.GetComponent<movemnt>().enabled = false;
 newPlayer = currentPlayer;
 characterSelect.SetActive(false);
 }
 else if (TheScore.thePoints < yellowPlaneCost && yellowPlaneSave == 0)
 {
 Debug.Log("Not enough coins");
 }
 }

 

I think I need to save the currentPlayer GameObject But I am unsure on how to do this. Or if there is another way to fix this.

 

Thanks!

  • Liked by
Reply
0 on November 13, 2018

There are multiple ways to do this. Make the character static or as a singleton, if you only have one or save the index for the correct character model as a “PlayerPref” and load it after your scene gets reloaded. These things will survive your scene reload.

 

  • Liked by
Reply
Cancel