Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 unity_596D03932D4F906C4A99 · Oct 26, 2021 at 03:18 AM · loadjsonsaveload

Saving JSON didnt work

Im trying to create save and load script, the loading is working, but the saving is not working. I dont know what is wrong with my script, can someone help me?

 using UnityEngine;
 using System.IO;
 
 [System.Serializable]
 public class PlayerData
 {
     public int skor = score.skorupdate;
 
 }
 public class SaveSystem : MonoBehaviour
 {
     [SerializeField] private PlayerData data = new PlayerData();
     public void SavePlayer()
     {
         string json = JsonUtility.ToJson(data);
         File.WriteAllText(Application.persistentDataPath + "/save.json", json);
 
         Debug.Log("Saved!");
     }
 
     public void LoadPlayer()
     {
         if (File.Exists(Application.persistentDataPath + "/save.json"))
         {
             string json = File.ReadAllText(Application.persistentDataPath + "/save.json");
             PlayerData data = JsonUtility.FromJson<PlayerData>(json);
 
             score.skorupdate = data.skor;
 
             Debug.Log("Loaded!");
         }
         else
         {
             Debug.Log("File Not Found In" + Application.persistentDataPath + "/save.json");
         }
     }
 }
Comment
Add comment · Show 2
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 rh_galaxy · Oct 26, 2021 at 06:23 AM 0
Share

We need to know where you keep the score variable. Try debugging it by doing

 Debug.Log("Saved! skor " + data.skor);
avatar image unity_596D03932D4F906C4A99 rh_galaxy · Oct 26, 2021 at 06:27 AM 0
Share

Found it, here. I dont know why its always on 0

 {"skor":0}

1 Reply

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

Answer by rh_galaxy · Oct 26, 2021 at 06:46 AM

It is 0 because score.skorupdate is 0 at start where you create data, you don't update it when you do the save. We need code for the score variable, but you can try this:

 //[SerializeField] private PlayerData data = new PlayerData(); //remove this line
 public void SavePlayer()
 {
     PlayerData data = new PlayerData(); //and put it here
     string json = JsonUtility.ToJson(data);
     File.WriteAllText(Application.persistentDataPath + "/save.json", json);
 
     Debug.Log("Saved! skor " + data.skor);
 }
Comment
Add comment · Show 1 · 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 unity_596D03932D4F906C4A99 · Oct 26, 2021 at 07:26 AM 0
Share

Thanks, It's worked. I hope you have a good day

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

133 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

Related Questions

Playerprefs doesn't save anything. 2 Answers

Save and load a class 0 Answers

Why is JSON not saving my Class? 2 Answers

LitJson load float 2 Answers

Save and load system not working please help 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