Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by Banditmayonnaise · Feb 07, 2021 at 09:43 PM · 2d-platformersave datasaveloadloading filesave file

How do i Make this saving system apply the save data in my game.

Hi I followed the Brackerys save and load tutorial, and I thought I would implement something similar in my game. But I don't know how to tell the game to use the specific variables as data.

So I'm currently making the Main Menu Scene where the player can change Sounds, music, and other settings. I just want players' data, so when the player starts the game Music and Sound will be at the same levels and if the player reopens the game, they should have the same values.

What I tried: Here's the SettingsData Script. This script controls the volume sliders and the toggle buttons.

   [Header("Options")]
     [SerializeField]
     private AudioMixer audioMixer;
     [Space]
     public bool speedRunClock; //Public becuase this is data that needs to be saved when the game is starting & if true the game will start with a timer
     public bool fullScreenMode; //If true then the game will be played in fullsreen
     public bool cameraShakeEffect; //If true then there will be camera shake effect in the game
     [Space]
     public float currentVolumeForMaster;
     public float currentVolumeForSoundFX;
     public float currentVolumeForMusic;
     // Start is called before the first frame update
     void Start()
     {
         //If there is no data don't load this
         LoadSettingsData();
 
 
     }
 
     #region OptionsMenu
     //This might not be needed
     //This function is done in MessySlider Script
     public void SetMasterVolume(float volume)
     {
         audioMixer.SetFloat("MasterVolume", Mathf.Log10(volume) * 20);
 
         volume = currentVolumeForMaster;
     }
     public void SetSoundFXVolume(float SoundsVolume)
     {
         audioMixer.SetFloat("soundsVolume", Mathf.Log10(SoundsVolume) * 20);
 
         SoundsVolume = currentVolumeForSoundFX;
 
     }
 
     public void SetMusicVolume(float musicVolume)
     {
         audioMixer.SetFloat("musicVolume", Mathf.Log10(musicVolume) * 20);
 
         musicVolume = currentVolumeForMusic;
     }
 
     public void SetFullScreen(bool isFullScreen)
     {
         Screen.fullScreen = isFullScreen;
 
         fullScreenMode = isFullScreen;
     }
 
     public void SpeedRunClockActivated(bool _SpeedRunClock)
     {
         speedRunClock = _SpeedRunClock;
 
     }
 
     public void CameraShakeEffect(bool isCameraShakeEffect)
     {
         cameraShakeEffect = isCameraShakeEffect;
     }
 
     #endregion
 
     public void SaveSettingsData()
     {
         SaveSystem.SavePlayerPreferences(this);
     }
 
     public void LoadSettingsData()
     {
         SavingSettingsData savingSettingsData = SaveSystem.LoadPlayerPreferences();
 
         //Bool values
         speedRunClock = savingSettingsData.speedrunClock_Save;
         fullScreenMode = savingSettingsData.fullScreenMode_Save;
         cameraShakeEffect = savingSettingsData.cameraShake_Save;
 
         //Slider aka the Float value
         currentVolumeForMaster = savingSettingsData.masterVolume;
         currentVolumeForMusic = savingSettingsData.musicVolume;
         currentVolumeForSoundFX = savingSettingsData.soundFXVolume;
     }

I then have another script where I will create classes that use the data above and store it in different variables.

 [System.Serializable]
 public class SavingSettingsData
 {
 
     public float masterVolume;
     public float musicVolume;
     public float soundFXVolume;
     public bool speedrunClock_Save;
     public bool fullScreenMode_Save;
     public bool cameraShake_Save;
 
     public SavingSettingsData(SettingsDataManager settings)
     {
         //This will only work if the data from SettingsDataManager is converted to currentvolumeversion
         masterVolume = settings.currentVolumeForMaster;
         musicVolume = settings.currentVolumeForMusic;
         soundFXVolume = settings.currentVolumeForSoundFX;
 
         //public bools
         speedrunClock_Save = settings.speedRunClock;
         fullScreenMode_Save = settings.fullScreenMode;
         cameraShake_Save = settings.cameraShakeEffect;
 
 
 
     }
 }

Now for the last script, I wanted to create a saving system that actually saves all the data on the computer.

 public static class SaveSystem
 {
 
     public static void SavePlayerPreferences (SettingsDataManager settings)
     {
         BinaryFormatter formatter = new BinaryFormatter();
 
         string path = Application.persistentDataPath + "Player.Preferences";
         FileStream stream = new FileStream(path, FileMode.Create);
 
         SavingSettingsData settingsData = new SavingSettingsData(settings);
 
         formatter.Serialize(stream, settingsData);
         stream.Close();
     }
 
     public static SavingSettingsData LoadPlayerPreferences()
     {
         string path = Application.persistentDataPath + "Player.Preferences";
         if (File.Exists(path))
         {
             BinaryFormatter formatter = new BinaryFormatter();
             FileStream stream = new FileStream(path, FileMode.Open);
 
            SavingSettingsData savingSettingsData = formatter.Deserialize(stream) as SavingSettingsData;
             stream.Close();
 
             return savingSettingsData;
         }
         else
         {
             Debug.LogError("Save file not found in " + path);
             return null;
         }
     }
 }



I think the problem is currently in my SettingsData script because there is no time where I'm telling the program that for example, currentVolumeForMaster should be used as a volume. So my questions have can save the Player Settings Data and load it when switching to another scene or when the player reopens the game? And another quick question is; If this saves system works will the slider remember its slider value and fill the area accordingly?

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

0 Replies

· Add your reply
  • Sort: 

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

164 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

Related Questions

[Error:] Cannot Deserialize JSON to new instances of type ' X ' 1 Answer

How To Save and Load player position using Serialization? 0 Answers

Saveing and Loading Problem 0 Answers

Saving current progression question 0 Answers

Vroid Opening Incorrect Save,Vroid opens the model above the one I click 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