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
1
Question by Blink · Feb 12, 2019 at 10:05 AM · serializationloadmenusavingsave data

Saving system tutorial / Load through main menu

Hi all, I've been researching ways of saving/loading a game for a few days now and its likely my inability to connect the dots between all the different tutorials out there but Ive come up short in finding a "comprehensive" save game tutorial/system that uses a main menu and a player pause menu together.

I have no problem setting up the UI of a main menu and an in-game pause menu that contains the save button but im struggling to link them together.

What I've been looking for is a way of saving a players location, health and what scene they are in etc. and then loading this data from the main menu.

If someone could point me to a tutorial or a free asset (as I'm a broke student) that could light the way for me that would be great! Thanks.

Comment
Add comment · Show 4
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 The_Icaruz · Feb 12, 2019 at 10:33 AM -1
Share

For an easy and simple way you could use PlayerPrefs.

Docs to PlayerPrefs

In the save function you use PlayerPrefs.Set() with the name and the value you want to save And the PlayerPrefs.Save() function

And in the load function on the $$anonymous$$ain menu you would use PlayerPrefs.Get()with the names and values you want to load.

Hope this helps.

avatar image Blink The_Icaruz · Feb 12, 2019 at 11:15 AM 0
Share

Thanks for the reply, I'm wanting a more in-depth explanation/tutorial on the matter though/ I'm unsure on how the saving script would find the data that needs saving. Would it be wise to have a playerdata script that holds all the players info (health, position etc) and get the save script to read from that?

avatar image The_Icaruz Blink · Feb 12, 2019 at 12:39 PM 0
Share

Why the downvote for the comment? On the big wide web there are tons of tutorials/explanations about saving/loading systems with PlayerPrefs or other solutions. At the moment I can't really explain it very well to you but you don't need an extra script to store all of your data, but you could if you want.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by blueshark- · Feb 12, 2019 at 04:57 PM

You can save by using PlayerPrefs (not recommended for lots of uses in a short amount of time). I saw The_Icaruz mention it so I'll mention saving with Binary.

Your script has to have these on top:

using System;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO; IO Standing for Input-Output

     void SaveData()
     {
         BinaryFormatter bf = new BinaryFormatter(); // Create new Binary formatter called bf
         FileStream file = File.Open(Application.persistentDataPath + "/gameInfo.dat", FileMode.Open);   // Create a file stream called file and open the file called gameInfo with .dat extension
 
         GameData data = new GameData(); // Create Game data called data
         data.highScore = highScore; // highScore variable found in the gameInfo.dat file is now set to highScore variable found in the script
 
         bf.Serialize(file, data);   // Write the stuff down > take data and write it to data
         file.Close();   // close file > We're not manipulating with it anymore
     }
 
     void LoadData()
     {
         if(File.Exists(Application.persistentDataPath + "/gameInfo.dat"))   // If the file exists
         {
             BinaryFormatter bf = new BinaryFormatter(); // Create new Binary formatted called bf
             FileStream file = File.Open(Application.persistentDataPath + "/gameInfo.dat", FileMode.Open);   // Open file with name gameInfo with .dat extension
             GameData data = (GameData)bf.Deserialize(file); // Pulling the info out of the file. (GameData) casts it from a generic object to GameData specifically
             file.Close(); // Close the file > We don't need it anymore
 
             highScore = data.highScore; // Set the script's highScore to the highScore variable found in the data folder
 
         }
     }

Sorry if the code looks cluttered :). This is a basic version of it working, you can obviously change stuff on it to make it work as you wish to.

Comment
Add comment · 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

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

105 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

Related Questions

Add an array to a class that has been Serialized with BinaryFormatter 1 Answer

Error when loading saved date : SerializationException: End of Stream encountered before parsing was completed. 2 Answers

im getting a serialization exception eror in my load method 0 Answers

Saving data through FileStream DataInfo? Think i'm misunderstanding something. 1 Answer

How are unity references serialized? (instanceID related) 1 Answer


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