Search Unity

i want to reset the level and keep the coin amount and the lose counter in the same value but when i

Discussion in 'Getting Started' started by thefrogeitan, Oct 20, 2019.

  1. thefrogeitan

    thefrogeitan

    Joined:
    Oct 20, 2019
    Posts:
    7
    i want to reset the level and keep the coin amount and the lose counter in the same value but when i restart the level the 2 variable reset too (i use playerprefs)
    Code (CSharp):
    1. using UnityEngine.UI;
    2. using UnityEngine;
    3.  
    4. public class coinText : MonoBehaviour
    5. {
    6.      public int coinAmount;
    7.  
    8.    
    9.     public Text text;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         text = GetComponent<Text>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         changetext();
    22.     }
    23.     public void changetext()
    24.     {
    25.         text.text = coinAmount.ToString() + " coins";
    26.     }
    27. }
    28.  
    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    2. using UnityEngine;
    3.  
    4. public class GameManagement : MonoBehaviour
    5. {
    6.     bool GameHasEnded = false;
    7.     public float restartDelay = 2f;
    8.     public GameObject completeLevelUI;
    9.     public coinText ct;
    10.     public int LoseCounter;
    11.     public EndTrigger et;
    12.     public int bank;
    13.  
    14.    
    15.  
    16.     public void EndGame()
    17.     {
    18.        
    19.         if (GameHasEnded == false )
    20.         {
    21.            
    22.             LoseCounter++;
    23.          
    24.             Invoke("Restart", restartDelay);
    25.             GameHasEnded = true;
    26.             ct.changetext();
    27.            
    28.            
    29.         }
    30.     }
    31.  
    32.     public void Restart()
    33.     {
    34.         GameHasEnded = false;
    35.         savedata();
    36.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    37.         loaddata();
    38.     }
    39.     public void CompleteLVL()
    40.     {
    41.         completeLevelUI.SetActive(true);
    42.     }
    43.     public void savedata()
    44.     {
    45.         PlayerPrefs.SetInt("coinamount", ct.coinAmount);
    46.         PlayerPrefs.SetInt("losecounter", LoseCounter);
    47.         PlayerPrefs.SetInt("level1", et.lvone);
    48.         PlayerPrefs.SetInt("level2", et.lvtow);
    49.         PlayerPrefs.SetInt("level3", et.lvthree);
    50.         PlayerPrefs.SetInt("BANK", bank);
    51.         PlayerPrefs.Save();
    52.     }
    53.     public void loaddata()
    54.     {
    55.         ct.coinAmount = PlayerPrefs.GetInt("coinamount");
    56.         LoseCounter = PlayerPrefs.GetInt("losecounter");
    57.         et.lvone = PlayerPrefs.GetInt("level1");
    58.         et.lvtow = PlayerPrefs.GetInt("level2");
    59.         et.lvthree = PlayerPrefs.GetInt("level3");
    60.         bank = PlayerPrefs.GetInt("BANK" + ct.coinAmount);
    61.         Debug.Log(ct.coinAmount.ToString());
    62.         Debug.Log(bank.ToString());
    63.  
    64.     }
    65.  
    66.  
    67.  
    68. }
    69.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Why in Restart() are you saving and then loading the same data? You should wait until your scene has reloaded before you load the data if you're going to take this approach. As written you are saving data, scheduling a scene reload, then loading your data back, then on the next frame the scene actually loads which probably is wiping out the data you just loaded.

    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html