Search Unity

PlayerPrefs issue!

Discussion in 'Scripting' started by uistudios, Nov 29, 2012.

  1. uistudios

    uistudios

    Joined:
    Apr 6, 2009
    Posts:
    46
    I am trying to add this code to my game object which serves as an icon for level 2 lock/unlock on my level select screen!
    Please help me figure out why this code is not responding to my PlayerPrefs in the 1st line
    i am trying to set it where i can see the code lock and unlock before actually implementing the PlayerPrefsInt into the game levels...


    Code (csharp):
    1.  
    2.  
    3. PlayerPrefs.SetInt("level02Unlocked", 1);//this will be moved to the end of level 2, its only here for testing!
    4.  
    5.  
    6.  
    7.  
    8. var Level02Lock : GameObject; // link this to the correct level lock icon!
    9. private var level02Unlocked = PlayerPrefs.GetInt("level02Unlocked"); //set this to the SAVED DATA so we can check if this is locked or unlocked!
    10.  
    11.  
    12.  
    13.  
    14. function Start () {
    15.  
    16.    
    17.     if (level02Unlocked == 1){ //1 means unlocked, player has beat this level!
    18.    
    19.         Level02Lock.gameObject.SetActive (false);//hide the lock since we already beat this
    20.        
    21.      }
    22.      
    23.     else if(level02Unlocked == 0){ //0 means lock it! its brand new...
    24.    
    25.         Level02Lock.gameObject.SetActive (true);//show the lock since we never beat this level!
    26.  
    27.      }
    28.    
    29.    
    30.  
    31. }
    32.  
    33.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You should not run code outside functions; it will only be executed once to set the variable up. Always put code inside functions. Only variable declarations should be outside, so it would be "private var level02Unlocked : boolean;".

    --Eric
     
  3. BlankFoxGirl

    BlankFoxGirl

    Joined:
    Apr 26, 2010
    Posts:
    71
    As Eric5h5 pointed out, you can't run code outside of a function, or you shouldn't run it.

    If you move "PlayerPrefs.SetInt("level02Unlocked", 1);//this will be moved to the end of level 2, its only here for testing!" to the Start function it should work fine.

    I would also recommend during testing, to change the following codes;

    PHP:
    private var level02Unlocked PlayerPrefs.GetInt("level02Unlocked");
    PHP:
    private var level02Unlocked;

    function 
    Start (){
    PlayerPrefs.SetInt("level02Unlocked"1);
    level02Unlocked PlayerPrefs.GetInt("level02Unlocked"); //This will return null or an error IF you execute the SetInt after this line of code for the first time you run the script.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You don't want to do that...you must always include the type, or supply a value so the type can be inferred.

    --Eric
     
  5. uistudios

    uistudios

    Joined:
    Apr 6, 2009
    Posts:
    46
    I tried the above code from Sarmth and it works! Thank you for helping me with this!

    I have a newbie follow up question :)


    1. I am assuming this code/technique of setting storage variables using PlayerPrefs.SetInt would work on mobile devices. Please confirm if that is the case. The idea is to have the players come back and pick up where they left off on the map level select screen.

    2. Eric, I am not clear... if the code above works but you are saying "You don't want to do that..." can you please show me what would be a better way or the proper way to set this up? ....


    I am using Sarmth's code on start function, then running my switches in the update functions so i did modify my original set up! and that is what made it work...

    tnx again!!!
     
    Last edited: Nov 29, 2012
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes.

    I already did; see my first post in this thread.

    --Eric
     
  7. uistudios

    uistudios

    Joined:
    Apr 6, 2009
    Posts:
    46
    got it :)
    private var level02Unlocked : boolean;

    thank you for your help!
     
  8. BlankFoxGirl

    BlankFoxGirl

    Joined:
    Apr 26, 2010
    Posts:
    71
    Glad to help.

    Sorry Eric, I should have mentioned that you should specify a variable type when declaring a new variable, although, Javascript it's self is very versatile and will automatically adapt. Not sure if this is the case within Unity3D, I have a lot of experience with Javascript it's self, but my knowledge on how Unity3D parses the Javascript code and uses it is lacking.

    Best coding practice even for JS is to specify a variable type when declaring a new variable.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    We're talking about iOS here, so you must always declare the type, either explicitly or implicitly. Dynamic typing isn't allowed on iOS.

    "Javascript" in Unity is only vaguely related to Javascript, and is more properly called Unityscript. It would be better to think of it as being like ActionScript3.

    --Eric
     
  10. BlankFoxGirl

    BlankFoxGirl

    Joined:
    Apr 26, 2010
    Posts:
    71
    Thanks for that Eric. This information will most certainly help some people out :)