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

Help with Player Prefs "bug", it's urgent!

Discussion in 'Scripting' started by jorgegb1997, Jun 5, 2015.

  1. jorgegb1997

    jorgegb1997

    Joined:
    Apr 15, 2014
    Posts:
    56
    Hello!

    I have a Player Prefs level unlock system, and the system it´s working, but the system have a "bug"...

    When i play the level 1 i unlock the level 2, but if i play the level 1 again i unlock the level 3.... This is a big problem, because i don´t need to play the level 2 to unlock the level 3, i just need to play the level 1 twice.

    How can I fix this?

    I have 2 scripts, the player prefs and the game manager.

    Player Prefs:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerPrefs_Manager : MonoBehaviour {
    5.     public int Number_of_Levels = 1;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.         Debug.Log(PlayerPrefs.GetInt ("LevelsUnlocked"));
    11.  
    12.         //Adding the sound settings key
    13.         if (!PlayerPrefs.HasKey ("SoundSettings"))
    14.         {
    15.             PlayerPrefs.SetInt ("SoundSettings", 1);
    16.         }
    17.  
    18.         //Adding the key that holds the amount of levels the user has unlocked.
    19.         if(!PlayerPrefs.HasKey ("LevelsUnlocked"))
    20.         {
    21.             PlayerPrefs.SetInt ("LevelsUnlocked", 1);
    22.         }
    23.  
    24.  
    25.         //Adding the key that holds what game mode the user
    26.         //has selected.
    27.  
    28.         if(!PlayerPrefs.HasKey ("GameMode"))
    29.         {
    30.             PlayerPrefs.SetString("GameMode","Timed");  
    31.         }
    32.  
    33.         CreateStars ();
    34.  
    35.     }
    36.  
    37.  
    38.     void CreateStars()
    39.     {
    40.  
    41.         for(int i = 0; i < Number_of_Levels; i++)
    42.         {
    43.             if(!PlayerPrefs.HasKey("Level" + i.ToString() + "Stars"))
    44.             {
    45.                 PlayerPrefs.SetInt("Level" + i.ToString() + "Stars",0);
    46.             }
    47.  
    48.         }
    49.  
    50.  
    51.  
    52.     }
    53. }

    Game manager:
    Code (CSharp):
    1.  
    2. public class GameManager : MonoBehaviour {
    3.     public int LevelNumber;
    4.     public GameObject imgStar1;
    5.     public GameObject imgStar2;
    6.     public GameObject imgStar3;
    7.     public AudioClip audPickup;
    8.     public AudioClip audWin;
    9.     public AudioClip audCrash;
    10.     //Panels
    11.     public GameObject PausePanel;
    12.     public GameObject PlayingPanel;
    13.     public GameObject WinPanel;
    14.     public GameObject LosePanel;
    15.     public GameObject SettingsPanel;
    16.     public Text txtGraphics;
    17.     public Text MuteText;
    18.  
    19.     GameObject player;
    20.  
    21.     int StarsCollected;
    22.     // Use this for initialization
    23.     void Start () {
    24.         player = GameObject.FindGameObjectWithTag("Player");
    25.         Time.timeScale = 1;
    26.         StarsCollected = 0;
    27.  
    28.  
    29.         //Gets whether the sound is set to muted, and changes audio settings accordingly.
    30.         if (PlayerPrefs.GetInt("SoundSettings") == 1)
    31.         {
    32.             AudioListener.pause = false;
    33.             MuteText.text = "";
    34.         } else if (PlayerPrefs.GetInt ("SoundSettings") == 0)
    35.         {
    36.             AudioListener.pause = true;
    37.             MuteText.text = "/";
    38.         }
    39.  
    40.         txtGraphics.text = GetQualityName (QualitySettings.GetQualityLevel());
    41.     }
    42.  
    43.  
    44.  
    45. public    void CollectStar ()
    46.     {
    47.         audio.PlayOneShot (audPickup);
    48.         StarsCollected += 1;
    49.  
    50.         if (StarsCollected == 1)
    51.             imgStar1.SetActive (true);
    52.         if (StarsCollected == 2)
    53.             imgStar2.SetActive (true);
    54.         if (StarsCollected == 3)
    55.             imgStar3.SetActive (true);
    56.  
    57.     }
    58.  
    59.     public void CollectTime()
    60.     {
    61.         audio.PlayOneShot (audPickup);
    62.     }
    63.  
    64.  
    65. public void LevelWin ()
    66.     {
    67.         int lvlULTemp = PlayerPrefs.GetInt("LevelsUnlocked") + 1;
    68.         PlayerPrefs.SetInt("LevelsUnlocked",lvlULTemp);
    69.  
    70.         Time.timeScale = 0;
    71.         int tempStars = PlayerPrefs.GetInt ("Level" + LevelNumber.ToString () + "Stars");
    72.         audio.PlayOneShot (audWin);
    73.         if (tempStars < StarsCollected)
    74.         {
    75.             PlayerPrefs.SetInt ("Level" + LevelNumber.ToString () + "Stars", StarsCollected);
    76.         }
    77.  
    78.         PausePanel.SetActive (false);
    79.         PlayingPanel.SetActive (false);
    80.         WinPanel.SetActive (true);
    81.         LosePanel.SetActive (false);
    82.         SettingsPanel.SetActive (false);
    83.  
    84.         }
    85.  
    86.  
    87. public    void LevelLose()
    88.     {
    89.         player.audio.Stop();
    90.         audio.PlayOneShot (audCrash);
    91.         PausePanel.SetActive (false);
    92.         PlayingPanel.SetActive (false);
    93.         WinPanel.SetActive (false);
    94.         LosePanel.SetActive (true);
    95.         SettingsPanel.SetActive (false);
    96.         Time.timeScale = 0;
    97.         Debug.Log ("Level Lose");
    98.     }

    PS: I win the level when the "player" collides with the WinPanel.

    Some help?
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    You need to increment the unlocked level only if you've just finished to last unlocked level, right ?

    Code (CSharp):
    1. int lvlULTemp = PlayerPrefs.GetInt("LevelsUnlocked");
    2. if (lvlULTemp == LevelNumber)
    3.     PlayerPrefs.SetInt("LevelsUnlocked",lvlULTemp+1);
     
    jorgegb1997 likes this.
  3. jorgegb1997

    jorgegb1997

    Joined:
    Apr 15, 2014
    Posts:
    56
    Yes!

    I have to put the code in Gamemanager.cs script in line 70?

    Thank you :)
     
    DoomSamurai likes this.
  4. jorgegb1997

    jorgegb1997

    Joined:
    Apr 15, 2014
    Posts:
    56
    Thak you very much!

    It works! Thanks a lot :) :) :)