- Home /
Question by
theunsigned · Mar 07, 2019 at 02:07 PM ·
levellevelsunlock
stumped on level unlock
I followed a tutorial on level unlocking; it's supposed to unlock the next level if the player gets at least one star on the game. It works for level 1 but then just unlocks the next level if the level is played from level 2 on. I'm going crosseyed trying to figure this out and figured I'd just ask what my error is. Forgive the comments, I'm using them as I learn.
//fill information based on level class
public void FillList()
{
//Destroy(GameObject.FindWithTag("LevelButton"));
//get all components to add, for each variable in the list called LevelList
foreach(var level in LevelList)
{
//instantiate and populate each button, establish which information goes into it
GameObject newbutton = Instantiate(levelButton) as GameObject;
//connect new button prefab to levelbutton c# script and call new button gameobject "button"
LevelButton button = newbutton.GetComponent<LevelButton>();
//set button prefab text element to leveltext in inspector
button.LevelText.text = level.LevelText;
//if level text contains a 1, then make it unlocked and make it interactable
if(PlayerPrefs.GetInt("Level" + button.LevelText.text) == 1)
{
level.UnLocked = 1;
level.IsInteractable = true;
}
//if (PlayerPrefs.GetInt("Level" + button.LevelText.text+"_star") >= 1)
//{
// int NextLevel = cl + 1;
// PlayerPrefs.SetInt("Level" + NextLevel, 1);
// level.UnLocked = 1;
// level.IsInteractable = true;
//}
//update the button, set it as unlocked and interactable from level manager
button.unlocked = level.UnLocked;
button.GetComponent<Button>().interactable = level.IsInteractable;
//add a listener for onclick -try to call a function
button.GetComponent<Button>().onClick.AddListener(() => LoadLevels("Level" + button.LevelText.text));
//activate stars on each button, depending on playerprefs
if (PlayerPrefs.GetInt("Level" + button.LevelText.text + "_star") >= 1)
{
button.Star1.SetActive(true);
int NextLevel = cl + 1;
PlayerPrefs.SetInt("Level" + NextLevel, 1);
level.UnLocked = 1;
level.IsInteractable = true;
}
if (PlayerPrefs.GetInt("Level" + button.LevelText.text + "_star") >= 2)
{
button.Star2.SetActive(true);
}
if (PlayerPrefs.GetInt("Level" +button.LevelText.text + "_star") == 3)
{
button.Star3.SetActive(true);
}
//at last step, put new buttons under spacer parent, including transform info
newbutton.transform.SetParent(Spacer);
}
Saveall();
}
//let the button know if it's unlocked, save player prefs
//go through every button, check if it's locked or unlocked
void Saveall()
{
//if (PlayerPrefs.HasKey("Level1"))
//{
// return;
//}
//else
//{
//tag button prefab with tag and find the objects with that tag, make a gameobject array of them
GameObject[] allButtons = GameObject.FindGameObjectsWithTag("LevelButton");
//check each button
foreach (GameObject buttons in allButtons)
{
//get Level Button C# script component from prefab
LevelButton button = buttons.GetComponent<LevelButton>();
//find the right level based on level text, set as unlocked in player prefs
PlayerPrefs.SetInt("Level" + button.LevelText.text, button.unlocked);
}
//}
}
Comment
Your answer
Follow this Question
Related Questions
Level unlock system 1 Answer
Texturing rooms all the same? 1 Answer
PlayerPrefs not save my unlock button (Levels lock/unlock) 0 Answers
About Creating Levels 1 Answer