- Home /
Having issues with PlayerPrefs...
Hi all!
Im working on part of my game that allows the player to change the characters color. Ive been able to get all the variables to keep their values, but when i relaunch the game, I find that nothing has been saved at all. I have the script set so that automatically if there is no save data, the character will be white, but if that check is passed, then it will load the values saved for the respective RGB codes. If anyone can give some explanation on how this is not working and how to fix it, that would be amazing!
P.S. It is all written in C#
using UnityEngine;
using System.Collections;
public class PlayerColorChange : MonoBehaviour {
static float Red = 0.0F;
static float Green = 0.0F;
static float Blue = 0.0F;
static int FirstPlay = 10;
void Start() {
if(FirstPlay == 10) { //Checks if First Play through of game
Debug.Log("First Playthrough, Either Fresh Install or No Save Data");
Red = 1f;
Green = 1f;
Blue = 1f;
} else {
FirstPlay = 5;
Debug.Log("This Works, Something is not saving correctly!");
}
}
public void ChangeRed(float newRed) { //Changes Red Value
Red = newRed;
}
public void ChangeGreen(float newGreen) { //Changes Blue Value
Green = newGreen;
}
public void ChangeBlue(float newBlue) { //Changes Green Value
Blue = newBlue;
}
// Update is called once per frame
public void Update () {
GetComponent<SpriteRenderer>().color = new Color(Red, Green, Blue, 1f);
}
public void SaveData() {
FirstPlay = 5;
Debug.Log("Save Data Created");
PlayerPrefs.SetInt("FirstPlay", FirstPlay); //Saves the First Playthrough Value
PlayerPrefs.SetFloat("Red", Red); //Saves Red Value
PlayerPrefs.SetFloat("Green", Green); //Saves Green Value
PlayerPrefs.SetFloat("Blue", Blue); //Saves Blue Value
}
}
Answer by KaushikRahul · Apr 13, 2016 at 06:03 AM
Hi
You are missing
PlayerPrefs.Save();
method.
Place it after you have Set all your PlayerPref Variable, like:
public void SaveData() {
FirstPlay = 5;
Debug.Log("Save Data Created");
PlayerPrefs.SetInt("FirstPlay", FirstPlay); //Saves the First Playthrough Value
PlayerPrefs.SetFloat("Red", Red); //Saves Red Value
PlayerPrefs.SetFloat("Green", Green); //Saves Green Value
PlayerPrefs.SetFloat("Blue", Blue); //Saves Blue Value
//Here
PlayerPrefs.Save();
}
And in Start, where you want to load the values from PlayerPrefs, you must use :
void Start() {
//Load FirstPlay Value Here
FirstPlay = PlayerPrefs.GetFloat("FirstPlay", FirstPlay);
if(FirstPlay == 10) { //Checks if First Play through of game
Debug.Log("First Playthrough, Either Fresh Install or No Save Data");
Red = 1f;
Green = 1f;
Blue = 1f;
}
//Load RGB Values Here
Red = PlayerPrefs.GetFloat("Red", Red);
Green = PlayerPrefs.GetFloat("Green", Green);
Blue = PlayerPrefs.GetFloat("Blue", Blue);
}
Edit: I made changes to the Start(), so the Loading of PlayerPrefs becomes more efficient, By moving the loading PlayerPrefs code out of the "If" condition.
Don't forget to accept an answer if it answered your question.
Please accept an answer if it answered your question.
Oh I answered below but I didn't notice he didn't call PlayerPrefs.Save();. Lol I didn't even bother checking because I though he had it!
Answer by Azbo · Apr 13, 2016 at 04:11 AM
I am not sure if this will work because I just took a quick glance but maybe you should change your methods to public static void instead of public void. Because the problem could encounter when its being sent between methods. I hope this worked best of luck mate!
Your answer
Follow this Question
Related Questions
saving highscores 1 Answer
Saving with PlayerPrefs? How it works? 1 Answer
Saving and loading game with PlayerPrefs 1 Answer
Save Datasets at runtime 2 Answers