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 /
avatar image
0
Question by IvanBgd · Jul 06, 2021 at 09:29 PM · 3dmobilesave data

I have watched Brackeys Saving system video but its not working

This is for an android game. // General is the name of my script //

Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 [System.Serializable]
 public class PlayerData
 {
     public int red = 0;
     public int yellow = 0;
     public int green = 0;
     public int purple = 0;
     public int black = 0;
     public int white = 0;
     public int cyan = 0;
     public int pink = 0;
     public int orange = 0;
 
     public int Money;
     public int stars = 0;
 
 
     public int Skin01 = 0;
     public int Skin02 = 0;
     public int Skin03 = 0;
     public int Skin04 = 0;
     public int Skin05 = 0;
 
 
     public PlayerData (General player)
     {
         red = player.red;
         yellow = player.yellow;
         green = player.green;
         purple = player.purple;
         black = player.black;
         white = player.white;
         cyan = player.cyan;
         pink = player.pink;
         orange = player.orange;
 
         Money = player.Money;
         stars = player.stars;
 
         Skin01 = player.Skin01;
         Skin02 = player.Skin02;
         Skin03 = player.Skin03;
         Skin04 = player.Skin04;
         Skin05 = player.Skin05;
     }
 }

second script:

 using UnityEngine;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using System;
 
 public static class SaveSystem
 {
     public static void SavePlayer (General player)
     {
         BinaryFormatter formatter = new BinaryFormatter();
         string path = Application.persistentDataPath + "/GameSaveData.beans";
         FileStream stream = new FileStream(path, FileMode.Create);
 
         PlayerData data = new PlayerData(player);
 
         formatter.Serialize(stream, data);
         stream.Close();
     }
 
     public static PlayerData LoadPlayer()
     {
         string path = Application.persistentDataPath + "/GameSaveData.beans";
         if (File.Exists(path))
         {
             BinaryFormatter formatter = new BinaryFormatter();
             FileStream stream = new FileStream(path, FileMode.Open);
 
             PlayerData data = formatter.Deserialize(stream) as PlayerData;
             stream.Close();
 
             return data;
         } else
         {
             Debug.LogError("Save file not found in " + path);
             return null;
         }
 
     }
 
 }


my script:

 ...
 ...
     private void Awake()
     {
         LoadPlayer();
     }   
 ...
 public void SavePlayerFunc()
     {
         SaveSystem.SavePlayer(this);
     }
     public void LoadPlayer()
     {
         PlayerData data = SaveSystem.LoadPlayer();
 
         red = data.red;
         yellow = data.yellow;
         green = data.green;
         purple = data.purple;
         black = data.black;
         white = data.white;
         cyan = data.cyan;
         pink = data.pink;
         orange = data.orange;
 
 
         Skin01 = data.Skin01;
         Skin02 = data.Skin02;
         Skin03 = data.Skin03;
         Skin04 = data.Skin04;
         Skin05 = data.Skin05;
     }
 ...
     public void Continue()
     {
         ...//called by a button
         SavePlayerFunc();
     }
 ...

Comment
Add comment · Show 1
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 andrew-lukasik · Jul 06, 2021 at 07:40 PM 0
Share

Temporarily replace BinaryFormatter with JsonUtility. This will allow you to read saved file and inspect what exactly is being saved etc.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PronKill · Jul 07, 2021 at 01:44 PM

Just use PlayerPrefs. They work good. https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Comment
Add comment · Show 3 · 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 rage_co · Jul 07, 2021 at 02:01 PM 0
Share

player prefs can be bulky for big projects...im assu$$anonymous$$g that's why he followed the tutorial anyway because otherwise playerprefs are the first things to pop up when you google this stuff

avatar image PronKill rage_co · Jul 07, 2021 at 02:42 PM 1
Share

When I was searching for saving/loading, I've seen PlayerPrefs only after hours of searching because BinaryFormatter wasn't working like I wanted to.

avatar image rage_co PronKill · Jul 07, 2021 at 03:27 PM 0
Share

strange, i found like 100s of player prefs articles and some json solutions before landing on a binary formatter one

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

202 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

how i can make smooth rotation for mobile game? 1 Answer

nav mesh agent stop ScreenPointToRay after a while 0 Answers

Mobile Build and Playmode Terrain Collision Disparity 0 Answers

Speed of Gui Text vs 3D Text Mobile 1 Answer

How to control 2 axes of object in 3d world 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