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. Dismiss Notice

Save and load system doesn't work (playerprefs)

Discussion in 'Scripting' started by raileanu22dragos, Dec 24, 2021.

  1. raileanu22dragos

    raileanu22dragos

    Joined:
    Sep 3, 2021
    Posts:
    27
    my loading code on the player game object:
    Code (CSharp):
    1. void Awake()
    2.     {
    3.         if (PlayerPrefs.GetInt("save")==1 && PlayerPrefs.HasKey("save"))
    4.         {
    5.             Debug.Log("Loaded1");
    6.             object1.transform.position = new Vector3(-10, 2, 4);
    7.             transform.position = new Vector3(-10, 2, 4);
    8.         }
    9.         if (PlayerPrefs.GetInt("save")==2 && PlayerPrefs.HasKey("save"))
    10.         {
    11.             Debug.Log("Loaded2");
    12.             object1.transform.position = new Vector3(-10, 2, 27);
    13.             transform.position = new Vector3(-10, 2, 27);
    14.         }
    15.     }
    object1 is the player. I have white cubes that on collision save progress and then move the player to the location on awake, as seen in the script. Here is the save script on the cubes:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         PlayerPrefs.SetInt("save", 1);
    4.         PlayerPrefs.Save();
    5.         Debug.Log("Saved");
    6.     }
    THE PROBLEM is that they don't save consistently, and for some reason, it looks entirely random, sometimes it saves, sometimes it doesn't, or to the wrong cube, taking away progress that the player has made.
    This is the script on the three menu buttons that start the game:
    Code (CSharp):
    1. public void PlayGame ()
    2.     {
    3.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    4.     }
    5.     public void QuitGame ()
    6.     {
    7.         Debug.Log("Quit");
    8.         Application.Quit();
    9.     }
    10.     public void StartGame()
    11.     {
    12.         PlayerPrefs.DeleteAll();
    13.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    14.     }
    Start game deletes all progress, while play game should start the game normally, and awake the player script that loads the game.

    I don't know what is causing the problem, the player script, the cube script, or the button script, and I have no idea why it sometimes seems to work at random
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
  3. raileanu22dragos

    raileanu22dragos

    Joined:
    Sep 3, 2021
    Posts:
    27
    thing is, I don't have a small number of variables, and I have to use multiple scripts for multiple gameobjects for the save system that I am making. Scattering them all over is necessary, I want to know what I am doing wrong, maybe there is an error in my coding, logic that unity isn't viewing as a coding error.
     
  4. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Are you sure that your OnTriggerEnter is being called for the player only? It could being called by other object and causing the randomness you see.

    As a side note, you could pass a default value for any PlayerPrefs.Get and avoid using HasKey.

    Something like:
    Code (CSharp):
    1.     {
    2.         if (PlayerPrefs.GetInt("save", 1)==1)
    3.         {
    4.             Debug.Log("Loaded1");
    5.             object1.transform.position = new Vector3(-10, 2, 4);
    6.             transform.position = new Vector3(-10, 2, 4);
    7.         }
    8.         else if (PlayerPrefs.GetInt("save", 1)==2)
    9.         {
    10.             Debug.Log("Loaded2");
    11.             object1.transform.position = new Vector3(-10, 2, 27);
    12.             transform.position = new Vector3(-10, 2, 27);
    13.         }
    14.     }
     
  5. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Very inconvenient for the player to be saving during play time like that. Instead you should be storing that data in the ram, 'int mySave = ', and only save to the harddrive during a break in the game like a load or player selecting to exit.

    The problem could be OnTriggerEnter, I typically like use OnTriggerStay, or perhaps you need to use multiple keys and store multiple object's trigger events in an order to determine the proper representation on load.
     
  6. raileanu22dragos

    raileanu22dragos

    Joined:
    Sep 3, 2021
    Posts:
    27
    I have tried the other methods above, but they are either way too complicated, different from what I'm used to, or just plainly don't work for my project. What I ended on was this:
    Code (CSharp):
    1. if (PlayerPrefs.GetInt("save")==1 && PlayerPrefs.HasKey("save"))
    2.         {
    3.             Debug.Log("Loaded1");
    4.             object1.transform.position = object2.transform.position;
    5.         }
    It works only the first time the level is loaded. After the player dies and goes to the main menu, reloading the level scene puts him at the beginning with no progress saved. I even moved the if statement to void update to see where the problem lies. In update, it did not work either, even though it should have locked me at the position of object2 right?
    The debug works fine,so the problem is the line below it. Is it not working because object1 is the player, and there is a problem with the script for movement? That shouldn't be the case, because it works the first time it's loaded.Is there a problem with the scene loading?
     
  7. raileanu22dragos

    raileanu22dragos

    Joined:
    Sep 3, 2021
    Posts:
    27