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 JustACode · Feb 20, 2021 at 12:34 AM · save file

How do I clear a json save file when the user clicks on new game?

I am making a game and I want it so that if the user clicks on New Game it will clear the save file and they can start out from the beginning. How would I do that? I'll add my save scripts for reference.


using System.Collections; using System.Collections.Generic; using UnityEngine;

public class GameInfo : MonoBehaviour {

 public int Lives = 3;
 public int Score = 0;

 void Start()
 {
     LoadPlayer();
 }

 public void AddPoints()
 {
     Score += 10;
 }

 public void LoseLive()
 {
     Lives -= 1;
 }

 public void SavePlayer()
 {
     SaveSystem.SavePlayer(this);
 }

 public void LoadPlayer()
 {
     GameStatus data = SaveSystem.LoadPlayer();

     Lives = data.Lives;
     Score = data.Score;
 }

}


using System.Collections; using System.Collections.Generic; using UnityEngine;

[System.Serializable] public class GameStatus { public int Lives; public int Score;

 public GameStatus(GameInfo player)
 {
     Lives = player.Lives;
     Score = player.Score;
 }

}


using UnityEngine; using System.IO; using System.Runtime.Serialization.Formatters.Binary;

public static class SaveSystem {

 public static void SavePlayer(GameInfo player)
 {
     BinaryFormatter formatter = new BinaryFormatter();
     string path = Application.persistentDataPath + "/game.data";
     FileStream stream = new FileStream(path, FileMode.Create);

     GameStatus data = new GameStatus(player);

     formatter.Serialize(stream, data);
     stream.Close();
 }

 public static GameStatus LoadPlayer()
 {
     string path = Application.persistentDataPath + "/game.data";

     if (File.Exists(path))
     {
         BinaryFormatter formatter = new BinaryFormatter();
         FileStream stream = new FileStream(path, FileMode.Open);

         GameStatus data = formatter.Deserialize(stream) as GameStatus;
         stream.Close();

         return data;
     }
     else
     {
         Debug.LogError("Save file not found in " + path);
         return null;
     }
 }

}

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

1 Reply

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

Answer by Hellium · Feb 20, 2021 at 12:34 AM

  1. Make your SavePlayer method take a GameStatus as argument instead of a GameInfo

  2. Add a default constructor in your GameStatus class initializing Lives & Score to 0

  3. Change SaveSystem.SavePlayer(this); to SaveSystem.SavePlayer(new GameStatus(this));

  4. Call SaveSystem.SavePlayer(new GameStatus()); when the New Game button is clicked

Comment
Add comment · Show 16 · 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 JustACode · Feb 20, 2021 at 06:46 PM 0
Share

@Hellium hey I think I believe I did what you ask and it is returning some errors. The SaveSystem.SavePlayer(new GameStatus()); "new GameStatus" and the SaveSystem.SavePlayer(this); is not working in my GameInfo. What should I do?

Here this is the script I am talking about.


using System.Collections; using System.Collections.Generic; using UnityEngine;

public class GameInfo : MonoBehaviour {

 public int Lives = 3;
 public int Score = 0;

 void Start()
 {
     LoadPlayer();
 }

 public void AddPoints()
 {
     Score += 10;
 }

 public void LoseLive()
 {
     Lives -= 1;
 }

 public void SavePlayer()
 {
     SaveSystem.SavePlayer(this);
 }

 public void LoadPlayer()
 {
     GameStatus data = SaveSystem.LoadPlayer();

     Lives = data.Lives;
     Score = data.Score;
 }

 public void newGame()
 {
     SaveSystem.SavePlayer(new GameStatus());
 }

}

avatar image Hellium JustACode · Feb 20, 2021 at 06:50 PM 0
Share

It's almost impossible to help without the errors and the code throwing those errors.

Make sure you have added a default constructor to the GameStatus class = a constructor taking no argument.

You also need to change SaveSystem.SavePlayer(this); to SaveSystem.SavePlayer(new GameStatus(this));

avatar image JustACode Hellium · Feb 20, 2021 at 07:00 PM 0
Share

@Hellium okay I'll be honest with you I don't know what a default constructor is can you send me an example.

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

113 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

Related Questions

Save and Load 2 Answers

SaveStates in a platformer 0 Answers

persistantdata deleted on steam build 0 Answers

Save picture from camera with zoom 0 Answers

How would you send binary data over web? 2 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