Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED]C# Saving script only saves once.

Discussion in 'Scripting' started by traderain, Jul 16, 2015.

  1. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    Why is this not working?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Diagnostics;
    4.  
    5. public class Saver : MonoBehaviour {
    6.  
    7.     public Stopwatch time;
    8.     private bool isSaving = false;
    9.     private GameManager manager;
    10.  
    11.     void Awake()
    12.     {
    13.         manager = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameManager>();
    14.         time = new Stopwatch();
    15.         time = Stopwatch.StartNew();
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         if((isSaving)&&(time.ElapsedMilliseconds >= 5000))
    21.         {
    22.             time.Stop();
    23.             Save();
    24.             print("Saved");
    25.             time.Reset();
    26.             time.Stop();
    27.         }
    28.     }
    29.    
    30.      public void StartSave()
    31.     {
    32.         isSaving = true;
    33.     }
    34.     public void StopSave()
    35.     {
    36.         isSaving = false;
    37.     }
    38.     public void Save()
    39.     {
    40.         ES2.Save(manager.player_money,"money");
    41.         ES2.Save(manager.player_money,"money");
    42.         ES2.Save(manager.Player_damage,"player_damage");
    43.         ES2.Save(manager.player_xp,"player_xp");
    44.         ES2.Save(manager.xp_req,"xp_req");
    45.         ES2.Save(manager.player_armor,"player_armor");
    46.         ES2.Save(manager.player_health,"player_health");
    47.         ES2.Save(manager.player_potions,"potions");
    48.         ES2.Save(manager.player_armorpen,"player_armorpen");
    49.         ES2.Save(manager.player_level,"level");
    50.         ES2.Save(manager.boosterpacks,"boosterpacks");
    51.         ES2.Save(manager.Quest,"quest");
    52.         ES2.Save(manager.monster_xp_reward,"monster_xp_reward");
    53.         ES2.Save(manager.monster_damage,"monster_damage");
    54.         ES2.Save(manager.monster_armor,"monster_armor");
    55.         ES2.Save(manager.monster_armorpen,"monster_armorpen");
    56.         ES2.Save(manager.monster_health,"monster_health");
    57.         ES2.Save(manager.monster_gold_reward,"monster_gold_reward");
    58.         ES2.Save(manager.xp_req,"xp_req");
    59.         ES2.Save(manager.xp_req,"xp_req");
    60.         ES2.Save(manager.player_xp,"player_xp");          
    61.     }
    62. }
    63.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you don't appear to be calling StartSave() or StopSave() at any point
     
    traderain likes this.
  3. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    I do from my other script:
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Collections;
    4. using UnityEngine;
    5. using System.Diagnostics;
    6.  
    7. public class GameManager : MonoBehaviour
    8. {
    9.     private static GameManager _instance;
    10.     public Saver ment;
    11.    
    12.     public static GameManager instance
    13.     {
    14.         get
    15.         {
    16.             if(_instance == null)
    17.             {
    18.                 _instance = GameObject.FindObjectOfType<GameManager>();
    19.                
    20.                 //Tell unity not to destroy this object when loading a new scene!
    21.                 DontDestroyOnLoad(_instance.gameObject);
    22.             }
    23.            
    24.             return _instance;
    25.         }
    26.     }
    27.    
    28.     void Awake()
    29.     {
    30.  
    31.         if(_instance == null)
    32.         {
    33.             //If I am the first instance, make me the Singleton
    34.             _instance = this;
    35.             DontDestroyOnLoad(this);
    36.         }
    37.         else
    38.         {
    39.             //If a Singleton already exists and you find
    40.             //another reference in scene, destroy it!
    41.             if(this != _instance)
    42.                 Destroy(this.gameObject);
    43.         }
    44.         ment = gameObject.GetComponent<Saver>();
    45.         ment.StartSave();
    46.     }
    47.     void OnApplicationQuit()
    48.     {
    49.         ment.Save();
    50.     }
    51.  
    52. }
    53.  
    54.  
    55.  
    56.  
    57.  
    58.  
    59.  
    60.  
    61.  
    62.  
    63.  
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    is line 26 meant to be time.Start()?
     
  5. kemar

    kemar

    Joined:
    Nov 4, 2014
    Posts:
    15
    Hi,

    I don't know what's the behaviour of the classe Stopwatch...
    But i think your problem is here :

    Code (CSharp):
    1.  time.Stop();
    2.             Save();
    3.             print("Saved");
    4.             time.Reset();
    5.             time.Stop();
    6.  
    You stop your timer, so that's why you past only one time in the condition

    Code (CSharp):
    1. if((isSaving)&&(time.ElapsedMilliseconds >= 5000))
    2.        

    Try this

    Code (CSharp):
    1.  void FixedUpdate()
    2.     {
    3.         if((isSaving)&&(time.ElapsedMilliseconds >= 5000))
    4.         {
    5.             time.Stop();
    6.             Save();
    7.             print("Saved");
    8.             time.Reset();
    9.             time.StartNew();
    10.         }
    11.     }
    I hope that helps you.
     
    traderain likes this.
  6. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    Solved it.
     
    kemar likes this.