PlayerPrefs Autosave [SOLVED]
Hello, guys!
My aim is to set autosave and autoload for (my sort of) text-game. All I need to do is save Content position in Scroll.
I would really appreciate if someone could give a peek on my script ;)
The console doesn't throw errors, but nevertheless, the script doesn't work. In spite of that Debug.Log appears correctly. (I attach this script to Main Camera).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AutoSaveLoad : MonoBehaviour {
public GameObject content;
private void Start()
{
}
void OnEnable()
{
print("Application was loaded");
Load();
}
void OnApplicationQuit()
{
Save();
Debug.Log("Application was saved");
}
//Saving
public void Save()
{
PlayerPrefs.SetFloat("ContentX", content.transform.position.x);
PlayerPrefs.SetFloat("ContentY", content.transform.position.y);
Debug.Log("Content position is saved in PlayerPrefs");
}
//Loading
public void Load()
{
transform.position = new Vector2(PlayerPrefs.GetFloat("ContentX"), PlayerPrefs.GetFloat("ContentY"));
Debug.Log("Content position is loaded from PlayerPrefs");
}
}
Thank you!
thing i found your error, your doing new Vector2 on this.transform.position NOT the content.transform.position
if you want that game object to move, then get rid of your content variable and just use
this.gameobject.transform.postition x and this.gameobject.transform.position.y
Actually it is working as it should. your not debugging the info you want to see for 1 and 2, your not calling the data in the start or in OnEnable rather in this case to see if it did save. so how do you know it isn't working? all signs say yes it is.
@zereda-games I'm saying that it doesn't work because after I change Content position (scroll down) and quit Game mode -- on next load (starting Game mode again) the last position of Content doesn't load!
$$anonymous$$aybe I should add some strings particularly for displaying?..
Answer by Robert_Kirov · Feb 16, 2019 at 06:51 AM
Guys! All I had to do is attach this script not to Man Camera, but to Content Game Object itself. It works! Thank you all for responding and help! Cheerio!
But... just interesting. What's the essential difference?) @xxmariofer @zereda-games
Edit had to re-read convo, forgot the content was a GameObject. Having the public game object is not wrong, nor is it necessarily correct either. in most cases you don't want public. private Serialized Field's are far better.Ok, back to the question.. Well i don't know for certain but i think it have something to do with a rigidbody or something. camera cannot move an object if it dosen't have physics as far as i know With that being said I would assume the thing you are moving does have a rigid body. now if you had this script on the content GameObject it would be referencing it'self, the object with the physics in turn allowing it to move. if this is correct I'll make it an answer, otherwise it will remain here.
Answer by xxmariofer · Feb 15, 2019 at 08:34 AM
Hello, your key must be the same in the get and in the set. change your key since one is in caps and the other is not.
$$anonymous$$an! Such a humiliating mistake. But.. unfortunately, script still doesn't work (((
isnt working? you are not getting any value? are you calling playerprefs.clearall anytime? boths prints are showing fine? can you debug.log in the start for making sure it is getting saved? PlayerPrefs.GetFloat("ContentX") your code is fine.
@xxmariofer Debug.Log appears correctly. It looks kinda like this...
Your answer
Follow this Question
Related Questions
Saving item count with Playerprefs 1 Answer
Cannot Save due to file access denied WINDOWS 8 COMPUTER 0 Answers
Android Game Save and Load doesn't work 0 Answers
Playerpref Saving Lag 1 Answer
How to save progress? and not delete values on load 1 Answer