So I’m having this issue with PlayerPrefs not loading or saving as it seems.
I have been struggling with this and even looked up some examples but I think my issue might be the way it it set up I dunno. Maybe someone can have a look and tell me what I am doing wrong?
My script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SaveManager : MonoBehaviour {
//Script refference
public Experience experience;
public PlayerHealth playerHealth;
public LevelComplete levelComplete;
//Bools
public bool isLevelOneComplete = false;
public bool DidloadOnce = false;
//Variable to get shit working
public float maxPlayerHealth = 100f;
public int playerlevel = 1;
public float expNeeded = 100f;
public float currentExp = 0f;
public int levelOneComplete = 0;
public void SaveGame(){
Save();
}
public void LoadGame(){
Load();
}
void Awake(){
experience = FindObjectOfType<Experience>();
playerHealth = FindObjectOfType<PlayerHealth>();
DontDestroyOnLoad(transform.gameObject);
}
void Save(){
PlayerPrefs.SetFloat("MaxHealth", playerHealth.maxHealth);
PlayerPrefs.SetInt("PlayerLevel", experience.playerLevel);
PlayerPrefs.SetFloat("ExpNeeded", experience.expNeeded);
PlayerPrefs.SetFloat("CurrentExp", experience.currentExp);
if(levelComplete.LevelOneComplete == 1){
PlayerPrefs.SetInt("LevelOneComplete", levelOneComplete);
}
Debug.Log("Saved");
}
void Load(){
playerHealth.maxHealth = PlayerPrefs.GetFloat("MaxHealth");
experience.playerLevel = PlayerPrefs.GetInt("PlayerLevel");
experience.expNeeded = PlayerPrefs.GetFloat("ExpNeeded");
experience.currentExp = PlayerPrefs.GetFloat("CurrentExp");
levelComplete.LevelOneComplete = PlayerPrefs.GetInt("LevelOneComplete");
Debug.Log("Loaded");
}
}
The debug goes off so I assume the script completes but nothing happens. Any help would be appreciated and happy holidays everyone
I’m still waking up but, I’m not seeing where you are setting any values. For example, you declare and define maxPlayerHealth but you don’t assign it to playerHealth.maxHealth. Does playerHealth.maxHealth have a value yet that is being set somewhere else that I’m not aware of? Same goes for the rest of them.
Those are the save script and the one it is suppose to save values of. Currently it doesn’t seem to be doing anything as it isn’t loading so i assume it probably not saving aswell. Note that the save and experience script is set in a preloaded scene and doesn’t get destroyed and from there they search for the needed scripts. I don’t know if i explained that properly But yeah don’t know if that has anything to do with it.
The loading and saving are done with UI buttons. I have them on selected buttons. I play the game do a level and then the game is suppose to save. I stop playing and start again but it doesn’t load. So it either doesn’t load in the editor or im just doing something wrong for it not to work
Your save and load code is working for me. So, I don’t think it’s that.
To test, what I did was add a void Start() to your SaveManager.cs and called Save() and Load() then output to Debug.Log what was being returned from PlayerPrefs.
Maybe you can confirm that your save and load code works then once that is verified, go to the next step? Once you know for sure that you can save and load then move to how and when they are being called and go from there.
You said that you are seeing your debug statements? so the UI buttons are wired ok.
Yeah, your Exp script object isn’t getting instantiated. So when you call: experience.playerLevel=PlayerPrefs.GetInt(“PlayerLevel”); It can’t write to an object that isn’t there.
Case in point, when I tested, I added all the scripts to a cube object. So all of them were instantiated when the game started, meaning, you have to have the scripts (or at least one) attached to a GO.
EDIT: I confirmed that the data was writing to the registry on Windows where the data for PlayerPrefs is stored.
I added the experience script to a empty game object though. Or does it need to be something else? The level of frustration this script is causing me Seriously though ill add a screenshot.
I know everything should be working and I have gone over everything multiple times but this… this is just annoying. If all else fail mind if I send you the project so you can have a look at the source?
Did you apply the changes to the SaveManager prefab. it shows that you have unapplied changes.
EDIT: Sure, I’d be happy to take a look if needs be. I’ll PM you my email if that time comes. But, what I would say is a little struggle is good but don’t get frustrated. Unity is telling you the problem, the Exp object isn’t getting instantiated. Once you figure out why it isn’t, it’ll start working. Or at the very least, that part won’t be an issue anymore.
Just checked and apparently PlayerPrefs saves in the registry so i went and had a look but there wasn’t anything saved there so I’m thinking for some reason its not working with my Unity version.