Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tremayne · Mar 11, 2014 at 11:07 AM · javascriptplayerprefsscore systemgreyscale

How to add playerPrefs to this script?

Hi there

I know this is very newbie, but its really urgent that I get help with this please :)

I have a menu Scene that has 9 Gui buttons with attached Textures to them and when clicked they take you to the relevant level.However they are all unlocked which makes the game pointless!

How would I go about adding PlayerPrefs to allow a level unlocking system. So from start of game only level one is not Greyed Out. After completing Lvl 1 then Lvl 2 is un-greyed and clickable and so on.

Thanks in advance , I am really battling with PlayerPrefs.

Here's The JS script:

         public var button1 : Texture2D; 
 
         public var Button2 : Texture2D;
 
         public var Button3 : Texture2D;
 
         public var Button4 : Texture2D;
         
         public var Button5 : Texture2D; 
 
         public var Button6 : Texture2D;
 
         public var Button7 : Texture2D;
 
         public var Button8 : Texture2D;
         
         public var Button9 : Texture2D;
       
         public var Button10 : Texture2D;
         
         var btnTexture : Texture;
 
 
 
 
 
 
 
 
         
         
         // JavaScript
 function OnGUI () {
     // Make a background box
     
 
 
      
     // Make the first button. If it is pressed, Application.Loadlevel (1)
     
     GUI.Box (Rect (1,200,1300,480),"Trial Version V1.0 Copyrite of GenY Tech Limited ©");
     
       
            if (GUI.Button (Rect (10,230,200,200), button1)){
            
          Application.LoadLevel(3);
          
         }
     
             if (GUI.Button (Rect (225,230,200,200), Button2)){
             
         Application.LoadLevel (4);
         }
             if (GUI.Button (Rect (440,230,200,200), Button3)){
             
         Application.LoadLevel (5);
         }
             if (GUI.Button (Rect (655,230,200,200), Button4)){
         Application.LoadLevel (8);
         Handheld.Vibrate();
         
         }
        
             if (GUI.Button (Rect (870,230,200,200), Button5)){
         Application.LoadLevel (9);
         Handheld.Vibrate();
         
         }
         
         if (GUI.Button (Rect (1085,230,200,200), Button6)){
         Application.LoadLevel (10);
         Handheld.Vibrate();
         
         }
         if (GUI.Button (Rect (250,10,900,100), btnTexture)){
  
  
  Application.LoadLevel(1);
 
 }    
 
  
            if (GUI.Button (Rect (10,440,200,200), Button7)){
            
          Application.LoadLevel(11);
          
         }
     
             if (GUI.Button (Rect (225,440,200,200), Button8)){
             
         Application.LoadLevel (12);
         }
             if (GUI.Button (Rect (440,440,200,200), Button9)){
             
         Application.LoadLevel (13);
         }
             if (GUI.Button (Rect (655,440,200,200), Button10)){
         Application.LoadLevel (6);
         Handheld.Vibrate();
         
         } 
         
  
         
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · Mar 11, 2014 at 11:08 AM 0
Share

What PlayerPref things have you tried in your battle?

avatar image DanielJF · Mar 11, 2014 at 11:20 AM 0
Share

First off all you need to add something to the playerprefs: https://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html this can be a float int or a string (SetFloat,SetInt,SetString) then check foreach button(Level) if the playerpref is set(has$$anonymous$$ey). if it has the correct key you can show the button(by checking it) else you can not show it or whatever you want to do with it. Hope this helps.

Tip: You can also create all those buttons with a for loop.

avatar image Tremayne · Mar 11, 2014 at 11:51 AM 0
Share

I am sorry to do this

But do you have a sample of how I could use this in my script?

Thanks soo much

Tremayne

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by thornekey · Mar 11, 2014 at 11:16 AM

you could set an int (lets call it Lvl_1) to 0 for false and 1 for true.. then whenever you go to the menu have an if statement to check if PlayerPrefs.GetInt..etc if its 1 make it available.. if not keep greyed

now to make it greyed out and not actually usable you can do something like this:

             GUI.Label (new Rect (10,230, 200, 200), "Level 1!", "Button");
 

it jsut makes it look like an unclickable button :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tremayne · Mar 11, 2014 at 11:49 AM 0
Share

This looks like it would work but I am having problems integrating this to my script. I sorry for being a pain but how would I do this exactly?

Thanks Tremayne

avatar image thornekey · Mar 11, 2014 at 11:55 AM 0
Share

ok so a long-winded way of doing it from what we have from your code atm (i think.. on phone.. no compiler..)

     if (PlayerPrefs.GetInt("Level1Available") == 1) {
            if (GUI.Button (Rect (10,230,200,200), button1)){
  
          Application.LoadLevel(3);
            }
     }
     else if (PlayerPrefs.GetInt("Level1Available") == 0){
       GUI.Label (new Rect (10,230, 200, 200), button1, "Button");
     }


somethign like that i presume, but again, i cant check it in a compiler and its really late aha

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Im Doing HighScore System To My Game And There Is Something Wrong In My Scripts 0 Answers

PlayerPrefs Help 1 Answer

How would I transfer these into PlayerPrefs? 0 Answers

Is there a way to clear all the PlayerPrefs with names that start with a specific string? 1 Answer

Games played script using PlayerPrefabs 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges