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 DFiable · Nov 12, 2011 at 02:50 AM · buttonplayerprefssaveongui

Saving a button in PlayerPrefs

Is it possible to save a button in PlayerPrefs? I have 5 levels in my game. I would like to have a "Restart Level 2" button appear on the first scene of my project after the player reaches Level 2(Scene 2) and remains stored in PlayerPrefs just like my Highscore is saved in PlayerPrefs. Is this possible?
Below is my Highscore script using PlayerPrefs to store my highscore. I'd like to convert this script or add a function that will turn on a Level 2 Button. Maybe someone could point me in the right direction. I'm not sure how to do this. Thanx for any input!

 var r : float = 1;
 var g : float = 0;
 var b : float = 0;
 var a : float = 1;
 
 
 static var Score : int = 0;
 
 function Start(){
    
 
 
      Score+=100;
      guiText.text = "High Score: "+Score;
      
      var color : Color = Color (r, g, b, a);
           // Change the material to display green text.
         guiText.material.color = color;
         
         
     }
 
 function AddPoints() {
         
     Score+=100;
     guiText.text = "High Score: "+Score;
     
      
 } 
 
 
 
 function Awake() {
 Score = 0;
 }
 
 function Reset(){
 Score = 1200;
 }
 
 function ResetLevel3(){
 
 Score = 2100;
 }
 
 function ResetLevel4(){
 
 Score = 3200;
 }
 
 function ResetLevel5(){
 
 Score = 4400;
 }
 
 var HighestScore : int = 0;   
 var oldScore : int;
 
 
 function AddScore(){
      
    HighestScore = Score;
    
    for(i=0;i<10;i++){
       if(PlayerPrefs.HasKey(i+"HighScore")){
          if(PlayerPrefs.GetInt(i+"HighScore")<HighestScore){ 
             // new score is higher than the stored score
             oldScore = PlayerPrefs.GetInt(i+"HighScore");
             
             PlayerPrefs.SetInt(i+"HighScore",HighestScore);
             
             HighestScore = oldScore;
             
          }
       }else{
          PlayerPrefs.SetInt(i+"HighScore",HighestScore);
          
          HighestScore = 0;
          
       }
    }
 }
 
 
 var Score1 : int;
 
 
 function UpdateHScore(){
 
     Score1 = (PlayerPrefs.GetInt("0HighScore")); 
     
     guiText.text = "High Score: "+Score1;
     
 }


Below is my button script. "Quit" Button that appears after a player dies.

 // Draws 2 buttons, one with an image, and other with a text
 // And print a message when they got clicked.
 var btnTexture : Texture;
 function OnGUI() {
 if (!btnTexture) {
 Debug.LogError("Please assign a texture on the inspector");
 return;
 }
 
 if (GUI.Button(Rect(530,285,150,50),"QUIT"))
 Application.LoadLevel("sk9");
 gameObject.Find("GUIHighScore").SendMessage("AddScore");
 gameObject.Find("GUIHighScore").SendMessage("UpdateHScore");
 
 
 Time.timeScale =1;
 
 }



Again. I'm trying to save a(any) button in PlayerPrefs so that if the player returns to play the game a week later he or she can access the last level they played. Any suggestions would be appreciated. Thanx ps Please just make a "comment" if you don't have a specific answer. Thanx

Comment
Add comment · Show 2
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 syclamoth · Nov 12, 2011 at 04:03 AM 0
Share

By 'specific answer', do you mean 'write my script'?

avatar image DFiable · Nov 12, 2011 at 04:05 AM 0
Share

no. I mean don't send me a link to PlayerPrefs reference page.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Mox.du · Nov 12, 2011 at 04:11 AM

Hi,

you could save Level Name (using Application.loadedLevelName) as string, or Level Number (using Application.loadedLevel) as int to your PlayerPrefs using SetString for string name or SetInt for int number. Like This:

 PlayerPrefs.SetString("Level Name",Application.loadedLevelName); // for level name as string
 PlayerPrefs.SetInt("Level Name",Application.loadedLevel); // for level number


PlayerPrefs.

Since you are saving current loaded level when player dyes, it will save the name or level number of the current level loaded.

Then you can display a button which will load level name into the button's text from PlayerPrefs, and on click will load appropriate level, like this:

 if (GUI.Button(Rect(10,70,50,30),"Load "+ PlayerPrefs.GetString("Level Name")))
 Application.LoadLevel(PlayerPrefs.GetString("Level Name"));

or

 if (GUI.Button(Rect(10,70,50,30),"Load Level"+ PlayerPrefs.GetInt("Level Name")))
     Application.LoadLevel(PlayerPrefs.GetInt("Level Name"));

Regards

Comment
Add comment · Show 1 · 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 DFiable · Nov 12, 2011 at 04:16 AM 0
Share

Awesome! Thanx. I get it.

avatar image
1

Answer by syclamoth · Nov 12, 2011 at 04:07 AM

Well, you can store booleans in your PlayerPrefs as well- why don't you use a set of boolean values which tell the menu whether to activate the buttons? Alternatively, you could use an int for 'highest level reached' and set up the button using that instead.

so, having retrieved your button value-

 if(displayLevel2Button)
 {
     if(GUI.Button(whatever))
     {
         Application.LoadLevel("LEVEL");
     }
 }
Comment
Add comment · Show 4 · 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 DFiable · Nov 12, 2011 at 04:10 AM 0
Share

Well I don't realy understand, but thank you.

avatar image DFiable · Nov 12, 2011 at 04:12 AM 0
Share

When you say set up the button with an int. Can you give me an example. thanx

avatar image syclamoth · Nov 12, 2011 at 04:17 AM 0
Share

When I say set up the button with an int, I mean keep a counter of what level you are up to, store that in PlayerPrefs as I assume you already know how to do, and then draw a button like this-

 if(GUI.Button(Some Rect, "Load Level: " + levelNumber))
 {
     Application.LoadLevel(levelNumber + offset);
 }

Where offset is the number of 'splashscreen' / menu scenes before the actual game levels in the level list.

Can you tell me what parts you don't understand? I'll try to explain it best I can.

avatar image DFiable · Nov 12, 2011 at 04:20 AM 0
Share

Thanx I need to think about it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

PlayerPrefs Question 0 Answers

DeleteAll not working on android? 0 Answers

how to save a load a game? 1 Answer

PlayerPrefs script problem 2 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