Search Unity

Am I using PlayerPrefs wrong?

Discussion in 'Scripting' started by unity_Zam7EhCaU9M7QA, Jun 17, 2018.

  1. unity_Zam7EhCaU9M7QA

    unity_Zam7EhCaU9M7QA

    Joined:
    Mar 14, 2018
    Posts:
    4
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using UnityEngine.UI;
    4. public class GameManager : MonoBehaviour {
    5.  
    6.  
    7.     private Timer AccessTimerScript;
    8.     private GlobalManager AccessGlobalManager;
    9.  
    10.     private bool GameHasEnded = false;
    11.  
    12.     Text TimerText_obj;
    13.  
    14.     public float restartDelay = 2f;
    15.  
    16.     public GameObject completelevelUI;
    17.  
    18.     void Start()
    19.     {
    20.         Scene Currentscene = SceneManager.GetActiveScene();
    21.  
    22.         int buildIndex = Currentscene.buildIndex;
    23.  
    24.         if (buildIndex == 12)
    25.         {
    26.  
    27.             string highscorefloat_01 = PlayerPrefs.GetString("BestTime01");
    28.             AccessGlobalManager.Highscore_01.text = highscorefloat_01;
    29.  
    30.             string highscorefloat_02 = PlayerPrefs.GetString("BestTime02");
    31.             AccessGlobalManager.Highscore_02.text = highscorefloat_02;
    32.         }
    33.     }
    34.  
    35.     // loads the menu
    36.     void Update()
    37.     {
    38.         AccessTimerScript = GetComponent<Timer>();
    39.         AccessGlobalManager = GetComponent<GlobalManager>();
    40.  
    41.         if (Input.GetKeyDown(KeyCode.Escape))
    42.          {
    43.             SceneManager.LoadScene(0);
    44.         }
    45.  
    46.  
    47.     }
    48.  
    49.     // visar/loads "Level Complete" UI
    50.     public void LevelComplete()
    51.     {
    52.         completelevelUI.SetActive(true);
    53.  
    54.         SaveTime();
    55.  
    56.     }
    57.  
    58.     public void GameOver ()
    59.     {
    60.  
    61.         if (GameHasEnded == false)
    62.         {
    63.  
    64.             GameHasEnded = true;
    65.  
    66.             //restarts the game
    67.             Invoke("Restart", restartDelay);
    68.         }
    69.  
    70.     }
    71.  
    72.     void Restart()
    73.     {
    74.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    75.  
    76.     }
    77.  
    78.     public void SaveTime()
    79.     {
    80.  
    81.         Debug.Log("SaveTime metoden har regestrerats");
    82.  
    83.         Scene Currentscene = SceneManager.GetActiveScene();
    84.         int buildIndex = Currentscene.buildIndex;
    85.  
    86.  
    87.         if (buildIndex == 1)
    88.         {
    89.  
    90.             PlayerPrefs.SetString("BestTime01", AccessTimerScript.TimerString);
    91.  
    92.             Debug.Log("saved time for buildindex 1");
    93.  
    94.         }
    95.         if (buildIndex == 2)
    96.         {
    97.  
    98.             PlayerPrefs.SetString("BestTime02", AccessTimerScript.TimerString);
    99.  
    100.             Debug.Log("saved time for buildindex 2");
    101.  
    102.         }
    103.  
    104.     }
    105.  
    106. }  
    This is the error I am getting
    upload_2018-6-17_18-56-31.png
     
    Last edited: Jun 17, 2018
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Doug_B likes this.
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    A few suggestions:
    1. Please use code tags for code (Edit: Peter77 has just beat me to this point :)).
    2. Point 1 would allow us to link the error to the code.
    3. But even better- stick a breakpoint on line 95 of GameManager.cs and check all the variables on that line in the debugger. That will immediately highlight the exact problem to you.
     
  4. unity_Zam7EhCaU9M7QA

    unity_Zam7EhCaU9M7QA

    Joined:
    Mar 14, 2018
    Posts:
    4
    Thanks for your tips(Peter77 and Doug_B), I've had this error for 3 days now and I think I am using playerprefs wrong. I will have to find out how to use the debugger and breakspoints I guess.
     
    Doug_B likes this.
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I am not sure this is player prefs related. Are the two variables you're fetching with GetComponent actually on the same game object as this script?
    You should not try to use GetComponent in Update like that. Just put it in Awake if they're on the game object.
    If they are on some other game object, you should probably use a [SerializeField] attribute and drag n drop them in the inspector.

    Did you drag n drop the complete ui in the inspector?

    For future reference, if the line numbers differ in your post from the error message, it's helpful if you indicate the line number(s) in question from your post. :)
     
  6. unity_Zam7EhCaU9M7QA

    unity_Zam7EhCaU9M7QA

    Joined:
    Mar 14, 2018
    Posts:
    4
    methos5k,
    I am using DontDestroyOnLoad from a onother scene were I want the playerprefs to show a text, that's why I cannot use [SerializeField] and drag n drop in the inspector, therefor I have to use GetComponent? There is probably a better way of doing this. I am pretty new to unity3d and this is a way of the saving time from each level and show it in the level select menu. Thanks for helping.


    error line 91 is supposed to be line 90 and line 55 is supposed to be line 54, cannot edit post says it's spam.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You could save the score from each level, and then load them all on the level select menu.
    Just save the data to player prefs (one script per scene), and when you load the level select scene, read them all back and assign them to the text to show.

    Note: there are other ways to save data, like scores (JSON, binary formatter), but for now it's all good to get it working with player prefs just to get the logic working.
     
  8. unity_Zam7EhCaU9M7QA

    unity_Zam7EhCaU9M7QA

    Joined:
    Mar 14, 2018
    Posts:
    4
    Thanks for the help, this is a great forum.
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Line 90 throwing a nullref means that AccessTimerScript isn't assigned. You're assigning that through GetComponent, which means that there's no Timer script attached to the same GameObject as the GameManager.

    So there's two things to check:
    1: you have attached a Timer to the same object, and you're not destroying that somehow
    2: Check that there's no other GameManager scripts in the scene. It could be that you have set up everything correctly on the first one, but have another one you forgot, which is the one causing errors. You can check for all objects with a GameManager script on them by searching for "t:GameManager" in the hierarchy's search bar.