Search Unity

Question about unity save / load

Discussion in 'Scripting' started by KingSatch, Jul 7, 2019.

  1. KingSatch

    KingSatch

    Joined:
    Jul 7, 2019
    Posts:
    3
    Hey all,
    Im new to C# and Unity as a whole, and Im sure that this problem could be solved by someone with a bit of experience looking over this quickly, chalk it up to being a novice! My goal is to have the game save via save button and load to the exact state of when saved and continue on. theres some other stuff for the autos and upgrades, if required I will post these ASAP . Code is as follows:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.IO;
    6. using System.Runtime.Serialization.Formatters.Binary;
    7. using UnityEngine;
    8. using UnityEngine.UI;
    9. public class Game : MonoBehaviour
    10. {  //DiamondDisplay
    11.     public static int DiamondCount;
    12.     public GameObject DiamondDisplay;
    13.     public int InternalDiamond;
    14.     //CashDisplay
    15.     public static int CashCount;
    16.     public GameObject CashDisplay;
    17.     public int InternalCash;
    18.     [SerializeField]
    19.     private GameObject maingame;
    20.     [SerializeField]
    21.     private GameObject menu;
    22.     [SerializeField]
    23.     private bool isPaused = false;
    24.     void Update()
    25.     {
    26.         InternalDiamond = DiamondCount;
    27.         DiamondDisplay.GetComponent<Text>().text = "Diamonds: " + InternalDiamond;
    28.         InternalCash = CashCount;
    29.         CashDisplay.GetComponent<Text>().text = "Cash $: " + InternalCash;
    30.  
    31.         if (Input.GetKeyUp(KeyCode.Escape))
    32.         {
    33.             if (isPaused)
    34.             {
    35.                 Unpause();
    36.             }
    37.             else
    38.             {
    39.                 Pause();
    40.             }
    41.         }
    42.     }
    43.     void Start()
    44.     {
    45.         Cursor.lockState = CursorLockMode.Confined;
    46.     }
    47.     public void Pause()
    48.     {
    49.         maingame.SetActive(false);
    50.         menu.SetActive(true);
    51.         isPaused = true;
    52.     }
    53.     public void Unpause()
    54.     {
    55.         maingame.SetActive(true);
    56.         menu.SetActive(false);
    57.         isPaused = false;
    58.     }
    59.     public bool IsGamePaused()
    60.     {
    61.         return isPaused;
    62.     }
    63.  
    64.  
    65.    
    66.  
    67.     public void SaveGame()
    68.     {
    69.         // 1
    70.         SaveSystem save = CreateSaveGameObject();
    71.         // 2
    72.         BinaryFormatter bf = new BinaryFormatter();
    73.         FileStream file = File.Create(Application.persistentDataPath + "/SaveFile.MasterWare");
    74.         bf.Serialize(file, save);
    75.         file.Close();
    76.         // 3
    77.         DiamondDisplay.GetComponent<Text>().text = "Diamonds: " + InternalDiamond;
    78.    
    79.         Debug.Log("Game Saved");
    80.     }
    81.     public void LoadGame()
    82.     {
    83.         // 1
    84.         if (File.Exists(Application.persistentDataPath + "/SaveFile.MasterWare"))
    85.         {
    86.      
    87.             // 2
    88.             BinaryFormatter bf = new BinaryFormatter();
    89.             FileStream file = File.Open(Application.persistentDataPath + "/SaveFile.MasterWare", FileMode.Open);
    90.             SaveSystem save = (SaveSystem)bf.Deserialize(file);
    91.             file.Close();
    92.             // 4
    93.             DiamondDisplay.GetComponent<Text>().text = "Diamonds: " + save.InternalDiamond;
    94.      
    95.             Debug.Log("Game Loaded");
    96.             Unpause();
    97.         }
    98.         else
    99.         {
    100.             Debug.Log("No game saved!");
    101.         }
    102.     }
    103.     private SaveSystem CreateSaveGameObject()
    104.     {
    105.         SaveSystem save = new SaveSystem();
    106.         save.InternalDiamond= InternalDiamond;
    107.    
    108.         return save;
    109.     }
    110. }
    111.  
    112.  
    113.  
    114. //Save Script
    115.  
    116. using UnityEngine;
    117. using System.Collections;
    118. using System.Collections.Generic;
    119. [System.Serializable]
    120. public class SaveSystem
    121. {
    122.     public int InternalDiamond = 0;
    123.      //Will add cash number of stores ect.
    124. }
    125.  
    126.  
    I dont have any errors and the debug is telling me it is saving and loading but it simply just isnt doing anything. is again probably just incorrect and needing to be corrected, and if you could explain what Im doing wrong to correct for future use that would be awesome, cheers in advance all!!
    any recommendations or corrections/criticisms are welcome as long as accurate, thanks in advance all!!
     
    Last edited: Jul 7, 2019
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Update your post using code tags. I'm not sure anybody wants to look through that wall of text.
     
  3. KingSatch

    KingSatch

    Joined:
    Jul 7, 2019
    Posts:
    3
    Sorry, done!!
     
  4. KingSatch

    KingSatch

    Joined:
    Jul 7, 2019
    Posts:
    3
    Solved cheers!!