Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialUnsubscribed User
1,653 PointsMy C# code is not working (Unity, Level Unlocking)
Hi, I am fairly new to programming and I have one error/issue that I can not fix that I have spent 2 days straight trying to figure out. To give you a summary of what I am talking about, I am making a game where you unlock levels as you go for example level one is unlocked in the beginning but if you complete level 1 the level 2 button in the level select menu will be enabled and you can play level 2 etc. Everything works fine like you can reach all the way to level 7 etc. but when you replay a level like say you have unlocked the levels all the way to 7 and you decide to replay level 1, all the progress will get removed so if you complete level 1 only level 2 and level 1 is available, same if you replay level 3 and complete it all levels above 4's progress will be removed. I would be so grateful if someone could tell me how I can fix and edit my code so that it unlocks the levels as you go but you can still replay any level without the removing of any progress. So here is my 2 sets of code (More Info: I have 7 levels level 1 starts at "2" in the build index, there is a main menu, a level select, the levels and then credits. Its a block game where you are a block and you dodge other blocks, the scenefader script is just a script that fades between scenes.) :
using UnityEngine; using UnityEngine.SceneManagement;
public class LevelCompleted : MonoBehaviour {
public string nextLevel = "Level02";
public int levelToUnlock = 2;
public void LoadNextLevel()
{
PlayerPrefs.SetInt("levelReached", levelToUnlock);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class LevelSelector : MonoBehaviour {
public SceneFader fader;
// The buttons I added to a game object where I had 7 slots and I dragged in my buttons.
public Button[] levelButtons;
void Start()
{
int levelReached = PlayerPrefs.GetInt("levelReached", 1);
for (int i = 0; i < levelButtons.Length; i++)
{
if (i + 1 > levelReached)
{
levelButtons[i].interactable = false;
}
}
}
public void Select(string levelName)
{
fader.FadeTo(levelName);
}
}
1 Answer
Timothy Schmidt
4,806 PointsIt looks like you're not testing to see if the level had already been passed before setting your PlayerPrefs variable. In LoadNextLevel
, you could do a simple test to see if levelReached
is already higher than the level you want to set.
public void LoadNextLevel()
{
int currentUnlock = PlayerPrefs.GetInt("levelReached");
if (currentUnlock < levelToUnlock) {
PlayerPrefs.SetInt("levelReached", levelToUnlock);
}
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
Unsubscribed User
1,653 PointsUnsubscribed User
1,653 PointsIt works. Thank you so much.
Timothy Schmidt
4,806 PointsTimothy Schmidt
4,806 PointsWhat is the code that makes the buttons interactable after changing the
levelReached
?Edit: nm, it works. You're welcome