Having An Issue Getting Level Lock System To Function Properly
I followed this tutorial on implementing a level lock/unlock system into my game. link text
Everything else I used from this tutorial is working fine, like the level selector. But I am having an issue with the lock and unlock system portions. I am still able to click on the levels that are supposed to be locked and completing the levels does not remove the lock image like it is supposed to. I am not receiving any compile errors or anything like that. It's just not performing the way it is supposed to. I've looked at the comments on the page itself but was unable to find any answers. I've also looked up various other resources and even checked here on the questions forum but I am unable to find any solutions to help me.
I've also looked over the code myself several times and I cannot notice any errors, issues, or misspellings. I am still new to programming so I was hoping if anyone could help me figure out what the problem is? Here is my code for reference. public class LockLevel : MonoBehaviour { // Number of worlds public static int worlds = 2; // Number of levels public static int levels = 16; private int worldIndex; private int levelIndex; // Use this for initialization void Start () { // Erase data on start PlayerPrefs.DeleteAll(); // Call the function LockLevels LockLevels(); } // Function to lock the levels void LockLevels () { // Loop through all the levels of all the worlds for (int i = 0; i < worlds; i++) { for (int j = 1; j < levels; j++) { worldIndex = (i + 1); levelIndex = (j + 1); // Create a PlayerPrefs of that particular level and world and set it to 0, // if no key of that name exists if(!PlayerPrefs.HasKey("level"+worldIndex.ToString() +":" +levelIndex.ToString())) { PlayerPrefs.SetInt("level" + worldIndex.ToString() + ":" + levelIndex.ToString(), 0); } } } } }
Ok so here is the LevelSelect script.
public class LevelSelect : MonoBehaviour {
private int worldIndex;
private int levelIndex;
// Use this for initialization
void Start ()
{
// Create a temporary reference to the current scene
Scene currentScene = SceneManager.GetActiveScene();
// Retrieve the name of this scene
string sceneName = currentScene.name;
// Loop through all the worlds
for (int i = 1; i <= LockLevel.worlds; i++)
{
if (sceneName == "World1")
{
worldIndex = i;
CheckLockedLevels();
}
}
}
// Level to load button on click. Will be used for level button click event
public void Selectlevel(string worldLevel)
{
// Load the level
SceneManager.LoadScene("Level" + worldLevel);
}
// Load the MainMenu scene
public void MainMenu()
{
SceneManager.LoadScene("MainMenu");
}
// Load the next level select scene
public void Next()
{
SceneManager.LoadScene("World2");
}
// Load the previous level select scene
public void Previous()
{
SceneManager.LoadScene("World1");
}
// function to check for the levels locked
void CheckLockedLevels()
{
// Loop through the levels of a particular world
for(int j = 1; j < LockLevel.levels; j++)
{
levelIndex = (j + 1);
if ((PlayerPrefs.GetInt("Level"+worldIndex.ToString() +":" +levelIndex.ToString()))==1)
{
GameObject.Find("LockedLevel" + (j + 1)).active = false;
Debug.Log("Unlocked");
}
}
}
}
I noticed that on line 65 there is an issue that says GameObject.active is obsolete. I've attempted to fix this by using the recommended suggestions but trying those gave me the following errors: Using activeInHierarchy gave me the CS0200 error: Property or Indexer 'GameObject.activeInHierarchy' cannot be assigned to -- it is read only. Using activeSelf also returned the same error. Using SetActive gave me the CS1656 error: Cannot assign to 'SetActive' because it is a 'method group'.
These errors are the result of me attempting to fix it by replacing the last section of code with the respective suggestion, i.e. changing .active to .activeInHierarchy or .activeSelf. I know that I must change this but I am unsure of the appropriate way of typing out the line of code in the way that it is supposed to be. @Alanisaac
The tutorial you linked to describes using a LevelSelectScript. It looks like that portion is what locks users out of selecting levels. The code above does not include it, but are you using that script as well?
Yes I am using that script also. Would you like me to post it?
Your answer
![](https://koobas.hobune.stream/wayback/20220612174434im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to make a succession(n+1,5n,etc) that work in if statement? 0 Answers
Detect all object are touching as one cluster 0 Answers
how to make a succession(n+1,5n,etc) that work in if statement? 0 Answers
Rotating player around rotating planet,Rotation of player on a rotating planet 0 Answers
How to make a 3D bowling scoreboard for each 10 frames? 1 Answer