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
Your answer
![](https://koobas.hobune.stream/wayback/20220612062432im_/https://answers.unity.com/themes/thub/images/avi.jpg)