EvanMedi
EvanMedi

Reputation: 9

Unity3D - How to create a scene Holder for all levels and unlock some?

I'm new to unity and my programming capabitlities is still somewhat weak, I'm working on a 2d Platformer game as way to learn how to code meanwhile to practice my passion which is game development

my main issue right now is that I have two scenes(Level1,Level2) and then I have a SceneSelctor in the Selector I got two doors represent both levels, but the probleme I'm facing is althought

I created a little code to Lock the Level2 untill i reach a point and then unlock it,, is when I start the game the level2 is always unlocked which doesn't serves me well

the code that control this stuff is saved on two scripts I used Hastebin as I didn't know how to copy/paste the code in here

https://hastebin.com/oluzujukid.cs

===> LevelDoor.cs

public class LevelDoor : MonoBehaviour {

public string levelToLoad;

public bool unlocked;


// Use this for initialization
void Start () {

//first level must be always unlocked
    PlayerPrefs.SetInt("Level1", 1);

//when the leveltoLoad = 1 that mean that the level should be unlocked
    if(PlayerPrefs.GetInt(levelToLoad) == 1)
    {
        unlocked = true;
    } else
    {
        unlocked = false;
    }

//some animation stuff toshow doorOpen or closed
    if(unlocked)
    {
        doorTop.sprite = doorTopOpen;
        doorBottom.sprite = doorBottomOpen;
    } else
    {
        doorTop.sprite = doorTopClosed;
        doorBottom.sprite = doorBottomClosed;
    }
}

===> LevelExit.cs

public class LevelExit : MonoBehaviour {

public string levelToUnlock;
}


public IEnumerator levelExitCo()
{
    // if the player reach the checkpoint thisline unlock the second level on SceneSelect
    PlayerPrefs.SetInt(levelToUnlock, 1);
}

Upvotes: 0

Views: 294

Answers (1)

Ignacio Alorre
Ignacio Alorre

Reputation: 7605

This is how I would approach this issue:

Once the door in unlock I would set a trigger (just behind the opened door) which will load a new scene. Then to move from one scene to another I would use

SceneManager.LoadScene()

https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

In the second Level you can always place another door, already opened, with its trigger so the player can move back to the first level

Edit with another approach:

If you want to have a game with different levels that you can unlock sequentially, one thing you can do is to create a singleton variable, that is a unique variable that will be shared for all game scenes, of type array of booleans. Each element in the array will represent a level. Initially you can define all levels but the first one as false (so all locked) and when you consider a level can be unlocked you change that value

So at the beggining of your game you can call a scrip similar to (lets imagine you have 10 levels):

public static bool[] levels = new bool[10];
levels[0]=true
for (int i = 1; i < levels.Length; i++) 
{ levels[i] = false; }

Upvotes: 0

Related Questions