Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Crevi · Mar 25, 2016 at 05:54 PM · c#playerprefsloadmenusave data

PlayerPrefs: loading saved data from the menu?

I'm using a save script that saves the position of the player and which also has a load function. I call the load function in the menu scene, on a button ("continue game"), but it does not register. Any ideas on how to load data that has been saved in another scene from the menu?

Comment
Add comment · Show 8
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 Crevi · Mar 25, 2016 at 04:14 PM 0
Share

The data is saved when the player collides with a checkpoint. I've used the function "OnCollisionEnter".

avatar image hulahoolgames · Mar 25, 2016 at 05:56 PM 0
Share

can you post some code? that would be more clear as to where the problem lies.

avatar image Crevi hulahoolgames · Mar 25, 2016 at 10:13 PM 0
Share

It's for a school-project so I'm only allowed to ask very general questions I'm afraid. No posting of code, in other words.

avatar image Crevi · Mar 26, 2016 at 01:02 PM 0
Share

I'm certain that it saves, as I have already checked that in the way you said #Happy Zomby. It seems that the loading in the menu scene wants to happen as well, but I get a warning that "data is unavailable" and that the position data is "missing" (specifically: "GI output for input system: a bunch of letters is missing").

avatar image Happy-Zomby Crevi · Mar 26, 2016 at 04:42 PM 0
Share

Below is some code I use in my project in case it helps you find an issue

 // in my game setup scene
 var StartingScene : String;
 
 function StartGame()
 {
 PlayerPrefs.SetString("CompanyName",companyName.GetComponent(UI.Text).text);
 Application.LoadLevel(StartingScene);
 }
 
 // in my main level scene
 var companyName : GameObject;
 
 function Start ()
 {
 companyName.GetComponent(UI.Text).text = PlayerPrefs.GetString("CompanyName");
 }
 

do you have something like that?

avatar image Crevi Happy-Zomby · Mar 26, 2016 at 11:22 PM 0
Share

That does not look familiar. I just have something similar to what you wrote in your previous comment. A save function (OnCollisionEnter) that is called in my level scene when the player collides with a checkpoint/cube, and a load function (in the same script) that I have tried to attach to a "continue game" button in my menu scene without luck.

In my menu script I have no code that refers to the PlayerPrefs, I am simply trying to call the loadfunction (in the savescript) on the button itself through onclick.

Am I missing some obvious piece of code to make it work then or was your code only meant as an example? I'm quite new at this.

Show more comments
avatar image Crevi · Mar 27, 2016 at 09:52 PM 0
Share

That looks as if it would work #HappyZomby. I'm writing in C# though, but it looks quite similar so I'm sure there is an equivalent in C#.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Happy-Zomby · Mar 25, 2016 at 06:18 PM

Hi,

if you want to carry the save data from one scene to another you could used a don't destroy on load on the gameobject which holds your save data.

But I guess you mean more to load from the main menu... if so I believe you need to save to binary files... this gets fun because you can save game objects but just data.... so when you load you then need to re-dispatch this data to the different affected game objects in your scene. Here is a good starting point: http://www.sitepoint.com/saving-and-loading-player-game-data-in-unity/

If you do find another either way - please do share ! or perhaps I misunderstood what you are looking to do. good luck,

Comment
Add comment · 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 Crevi · Mar 25, 2016 at 10:16 PM 0
Share

I've added a don'tdestroyonload on the player but it doesn't do anything I'm afraid. So I'm guessing there is no way around changing to binary files if I want to load the players last position from the menu? I would really like to use playerPrefs if possible.

avatar image Happy-Zomby Crevi · Mar 25, 2016 at 11:05 PM 0
Share

If you are looking to load just the player position you can easily do that with player pref. This should work

 //in scene 1 on a save button
 var player : Transform;
     
 function Save ()
 {
 PlayerPrefs.SetFloat("PositionX", player.position.x);
 PlayerPrefs.SetFloat("PositionY", player.position.y);
 PlayerPrefs.SetFloat("PositionZ", player.position.z);
 }
 
 //you could launch this from a load button on scene 1
 //or you could launch in an awake function when scene 1 starts - e.g. if you choose continue from the main menu
 //you would have to reset the playerPrefs if you click new game - only have one save slot, except if you choose PositionXslot1, slot2, etc... and use a selectecor in the main menu for which one to load
 function Load ()
 {
 player.position = Vector3(PlayerPrefs.GetFloat("PositionX"),PlayerPrefs.GetFloat("PositionY"),PlayerPrefs.GetFloat("PositionZ"));
 }

hope that helps,

avatar image Crevi Happy-Zomby · Mar 25, 2016 at 11:41 PM 0
Share

As it happens, that is the exact code that I've got (only the save function is an OnCollisionEnter and I'm using C#), but it doesn't want to load from the menu even though I connect the load function to the "continue game" button in the menu.

You've given me some ideas though. I'll try to put it on awake and I'll also try to put it on a button ins$$anonymous$$d of using the OnCollisionEnter. Thanks !

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Saving system tutorial / Load through main menu 1 Answer

Is this a good way of resetting stats? 1 Answer

IS it possible to load the whole game (and all scenes) at startup to avoid scene change delay? 1 Answer

How do I save and load the state of the GameObjects with PlayerPrefs? 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