Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Amr12 · Feb 02, 2016 at 05:18 PM · playerprefslevelload

How can I set an array for the use of this playerpref code??

Im using a script here for a level unlock that grabs integers from some levels in my game. The integers are then used in the menu. If the saved integer is equivalent to 0 that means that the level has not been completed yet. Whereas when the savedlevel integer is set to 1 the level has been completed (as set from completion of the first level) unlocking the box collider in the menu. I have over 50 levels. That would mean that i would have 50 individual scripts for every level and a really long Menu script (since i have to fill in all the if's and else if's) how can i shorten this code and fit all 50 levels?

Here is the code;

 // MENULEVELLOCKER.js
 
 #pragma strict
 
 // This is where you plug in the GUITextures for the unlocked and the locked states for each level
 var level1Unlocked : GUITexture;
 var level1Locked : GUITexture;
 var level2Unlocked : GUITexture;
 var level2Locked : GUITexture;
 var level3Unlocked : GUITexture;
 var level3Locked : GUITexture;
 var Level1 : String;
 var Level2 : String;
 var Level3 : String;
 //These are the actual "buttons" that we use to click on to trigger an action to happen.
 //In our case they cause a level to be loaded depending on the collider you click on.
 //You will drag the collider button for each level into it's corresponding slot in the inspector.
 var level1Collider : GameObject;
 var level2Collider : GameObject;
 var level3Collider : GameObject;
 
 // This is where we create a variable slot for playerPrefs to plug in the set number for each level once it's won or not won.
 // playerPrefs will plug in "0" for a Not Won state and will plug in the level's number for the Won State.
 var levelReached1 : int = 0;
 var levelReached2 : int = 0;
 var levelReached3 : int = 0;
 
 // Let's grab the saved data for each level and grab that integer to use to load that level
 levelReached1 = PlayerPrefs.GetInt("Level1");
 levelReached2 = PlayerPrefs.GetInt("Level2");
 levelReached3 = PlayerPrefs.GetInt("Level3");
 
 function Update () 
 {    
 
     if(levelReached1 == 1) //if we've set the level won to 1 then disable the locked texture and enable the unlocked one
         {                      
             level1Unlocked.enabled = true; //remember Level1 is always unlocked by default...if not how will anyone play our game?
             level1Locked.enabled = false;
             level1Collider.SetActive(true);
             
             level2Unlocked.enabled = true;
             level2Locked.enabled = false;
             level2Collider.SetActive(true);
         }
       else if(levelReached1 == 0) //this is our case if the level you just played wasn't won yet.
         {
             level1Unlocked.enabled = true;
             level1Locked.enabled = false;
             level1Collider.SetActive(true);
             
             level2Unlocked.enabled = false;
             level2Locked.enabled = true;
             level2Collider.SetActive(false);
         }
     if(levelReached2 == 2) //if we've set the level won to 2 then disable the level 3 locked texture and enable the unlocked one
         {
             level3Unlocked.enabled = true;
             level3Locked.enabled = false;
             level3Collider.SetActive(true);
         }
     else if(levelReached2 == 0) //this is our case if the level you just played wasn't won yet.
         {
             level3Unlocked.enabled = false;
             level3Locked.enabled = true;
             level3Collider.SetActive(false);
         }
     if(levelReached3 == 3) //if we've set the level won to 3 then disable the locked texture and enable the unlocked one
         {
           //this is where Level 4 is supposed to go :)
         }
       else if(levelReached3 == 0) //this is our case if the level you just played wasn't won yet.
         {
     
         }
         
     if (Input.GetMouseButtonDown (0)) {
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition); //typical mouse click input
         var hit:RaycastHit;
     if (Physics.Raycast (ray, hit, 20)) {
         
         if(hit.collider.tag == "Finish") //this is all of our code for triggering loading levels when a collider is clicked on
         Application.LoadLevel(Level1);
         
         if(hit.collider.tag == "Finish")
         Application.LoadLevel(Level2);
         
         if(hit.collider.tag == "Finish")
         Application.LoadLevel(Level3);
         }
     }
 }

And here is the PlayerPref set for level 1

 function OnTriggerEnter(other : Collider) 
 {
     //
     if(other.gameObject.tag =="Player")
         {
             PlayerPrefs.SetInt("SavedLevel1", 1);
             Debug.Log("SavedLevel = 1");
             //
             yield WaitForSeconds(2.0); // wait for 5 seconds
             Application.LoadLevel("MainMenu");
         }
 }
 

Thanks so much ;)

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Level Unlock System....frustrated,Building a level unlock system using player prefs 0 Answers

how do i store highscore locally C# (SIMPLE) 3 Answers

PlayerPrefs help ! 1 Answer

How do I get a serialized field saved into a PlayerPref? 0 Answers

SetActive dosen't work 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