-2

after the level finishes the win screen is shown. which has two buttons continue and menu.i have managed to deactivate the buttons and only keep the 1st level button unlocked but cant get level 2 button to unlock when i clear level 1. also i want the continue button to jump to level 2 after it is shown in the next scene when completing level 1. this game is breakout style one and these are the problems that i am not able to solve. i have attached the respective scripts that i thought were necessary. if u want the others plz ask for them in the comments.a complete list of all scripts is at the end.i would really appreciate some help.i will definitely try to explain my problem a little more if u ask for them . so plz do check this question again later to checkout the changes.

the script below is attached to the level 1:-

{
[SerializeField] int breakableBlocks;  // Serialized for debugging purposes
SceneLoader sceneloader;

private void Start()
{
    sceneloader = FindObjectOfType<SceneLoader>();
}

public void CountBreakableBlocks()
{
    breakableBlocks++;
}

public void BlockDestroyed()
{
    breakableBlocks--;
    if (breakableBlocks <= 0)
    {
        GetComponent<LevelSelector>().levelunlocked = 
        sceneloader.LoadWinScreen();
    }
}
}

The script below is attached to level selector:-

{
    public Button[] levelButtons;
    public int levelunlocked = 1;
    private void Start()
    {
        int levelReached = PlayerPrefs.GetInt("levelReached", levelunlocked);
        for (int i = 0; i < levelButtons.Length; i++)
        {
            if (i + 1 > levelReached)
            {
                levelButtons[i].interactable = false;
            }
        }
    }
}

Scene loader script:-

public class SceneLoader : MonoBehaviour {


public void LoadNextScene()
{
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    SceneManager.LoadScene(currentSceneIndex + 1);
}
public void LoadStartScene()
{
    SceneManager.LoadScene(0);
}
public void LoadLevelSelectScreen()
{
    SceneManager.LoadScene(1);
}
public void LoadWinScreen()
{
    SceneManager.LoadScene(5);
}
public void LoadGameOverScreen()
{
    SceneManager.LoadScene(6);
}
public void QuitGame()
{
    Application.Quit();
}
public void Level1()
{
    SceneManager.LoadScene(2);
}
public void Leve2()
{
    SceneManager.LoadScene(3);
}
}

this is the build setting:-

build settings

The Scripts that i have :- 1.Ball 2.Block 3.Level 4.LevelSelector 5.LoseCollider 6.Paddle 7.SceneLoader

  • 2
    He Souvick Welcome to Stackoverflow. Please do not post your code as images. Also why not using `SceneManager.LoadScene` ? – Ali Kanat Mar 14 '19 at 11:05
  • sorry about that. i was copy pasting the code but for some reason it was stacking up side by side. anyway. i did think about using SceneManager.LoadScene but i have to add a lot of new things if i did that , which means spending again a lot of time in thinking how the code should work . i have already spent a lot of time on this idea , dont know if i should drop it for another. – Souvick Palit Mar 14 '19 at 11:39
  • i have deleted the photos and posted copied code this time. sorry for the delay – Souvick Palit Mar 14 '19 at 13:21
  • Yes but we still don't know how you load next scene. What is this `SceneLoader` script doing? – Ali Kanat Mar 14 '19 at 15:16
  • I would also take a look at how to format a good question in order to get good answers. Currently, you have no description of the problem in the question because it's in the title, and your title does not accurately describe your issue. [mcve] – Eliasar Mar 14 '19 at 16:13
  • ok wait a sec i will post the other scripts and also try to describe myself a little better – Souvick Palit Mar 14 '19 at 17:48
  • sorry for all the posting errors that i have made up till now. i hope this is satisfactory. – Souvick Palit Mar 14 '19 at 18:03

1 Answers1

0

I think your issue stems from not updating your "levelReached" player prefs value before leaving your level 1 scene.

Within your posted level 1 script:

public void BlockDestroyed()
    {
        breakableBlocks--;
        if (breakableBlocks <= 0)
        {
            GetComponent<LevelSelector>().levelunlocked = 
            sceneloader.LoadWinScreen();
        }
    }

The following line should be throwing an error as you're LoadWinScreen function returns void:

GetComponent<LevelSelector>().levelunlocked = 
            sceneloader.LoadWinScreen();

Try changing that section of code to the following:

if (breakableBlocks <= 0)
{
    PlayerPrefs.SetInt("levelReached", 2);
    sceneloader.LoadWinScreen();
}

Note in the above example I have assumed you have a separate script running game logic for each level as I am not using a variable to set the new PlayerPrefs "levelReached" value. I would recommend having a GameManager script that is present in each scene and tracks the level you are currently on, which would allow you to do the following:

if (breakableBlocks <= 0)
{
    if(PlayerPrefs.GetInt("levelReached") < GameManager.currentLevel + 1)
        PlayerPrefs.SetInt("levelReached", GameManager.currentLevel + 1);
    sceneloader.LoadWinScreen();
}

This requires some separate logic for carrying game state across scenes and there are several ways to go about this (see examples and relevant stackoverflow link below):

  • Singleton design pattern using the Unity DontDestroyOnLoad function
  • ScriptableObjects to store and retrieve data at level start
  • PlayerPrefs to store and retrieve data at level start

Unity - pass data between scenes