• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by Robert_Kirov · Feb 14, 2019 at 05:59 PM · playerprefssavescrollviewsaveload

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");
     }
 }

alt text alt text

Thank you!

2019-02-15-21-59-38.png (439.1 kB)
2019-02-15-22-00-56.png (30.4 kB)
Comment

People who like this

0 Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zereda-games · Feb 16, 2019 at 01:01 PM 0
Share

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

avatar image zereda-games zereda-games · Feb 16, 2019 at 03:05 AM 0
Share

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.

avatar image Robert_Kirov zereda-games · Feb 16, 2019 at 06:37 AM 0
Share

@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!

Maybe I should add some strings particularly for displaying?..

Show more comments
avatar image zereda-games zereda-games · Feb 16, 2019 at 01:06 PM 0
Share

It's like playing Where's Waldo LOL

2 Replies

  • Sort: 
avatar image
Best Answer

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!

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Robert_Kirov · Feb 16, 2019 at 06:54 AM 0
Share

But... just interesting. What's the essential difference?) @xxmariofer @zereda-games

avatar image zereda-games Robert_Kirov · Feb 19, 2019 at 07:44 PM 0
Share

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.

avatar image

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.

Comment
Robert_Kirov

People who like this

1 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Robert_Kirov · Feb 15, 2019 at 02:02 PM 0
Share

Man! Such a humiliating mistake. But.. unfortunately, script still doesn't work (((

avatar image xxmariofer Robert_Kirov · Feb 15, 2019 at 03:43 PM 1
Share

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.

avatar image Robert_Kirov xxmariofer · Feb 15, 2019 at 08:04 PM 0
Share

@xxmariofer Debug.Log appears correctly. It looks kinda like this...alt text

2019-02-15-21-59-38.png (439.1 kB)
2019-02-15-22-00-56.png (30.4 kB)
Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

174 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving item count with Playerprefs 1 Answer

How to save player score? (C#) 0 Answers

How can I save my options menu? 0 Answers

PlayerPrefs Not Saving When I Click the Button. 1 Answer

Saving In Between Scenes and Program Exit 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges