Still does not load the next level, gives me a win screen!

Version : Unity 5.5
I have tried every possible thing but its just not happening. All my scenes are there in the build settings and in the last 2 days i tried almost every possible combination. Nothing is working. I changed the “Application.LoadedLevel” as well to something else before getting it back again because nothing seems to work. The count is correct, i just did a debug and thats working fine. Its resetting to 0. but its not going to Level 2. Its either Win OR Lose. Please help me.
Here are the screenshots of my scripts which have something to do with this behavior :

Please Help me out here! I know im following it with Unity 5.5 but i came this far, cannot start over.

Hi,

Glad to see you have fixed your problem.

Just so you know, you can paste your code directly into your posts which can be a little easier than screenshots :slight_smile:


See also;

I actually did not solve it. i edited the win condition but now i realized that though it will go to the next level, after all the levels are over, it will not come out off it and go to the win screen. Please Help.

Refer :

and yes the code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class LevelManager : MonoBehaviour {

	public void LoadLevel(string name)
    {
        Debug.Log("load the level requested for : " + name);
        Brick.breakableCount = 0;
        SceneManager.LoadScene(name);
    }

    public void QuitRequest()
    {
        Debug.Log("i want to quit!");
        Application.Quit();
    }

    public void LoadNextLevel()
    {
        Brick.breakableCount = 0;
        SceneManager.LoadScene(Application.loadedLevel + 1);
    }

    public void BrickDestroyed()
    {
        if(Brick.breakableCount <= 0)
        {
            SceneManager.LoadScene(Application.loadedLevel + 1);
        }
       
    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Brick : MonoBehaviour
{
    public AudioClip crack;
    private int maxHits;
    public Sprite[] hitSprites;
    public static int breakableCount = 0;
    private bool isBreakable;

    private int timesHit;
    private LevelManager levelManager;

    // Use this for initialization
    void Start()
    {
        isBreakable = (this.tag == "Breakable");
        // keep track of breakable bricks
        if (isBreakable)
        {
            breakableCount++;
        } 
        timesHit = 0;
        levelManager = GameObject.FindObjectOfType<LevelManager>();
    }

    // Update is called once per frame
    void Update()
    {

    }



    void OnCollisionEnter2D(Collision2D col)
    {
        AudioSource.PlayClipAtPoint(crack, transform.position); 
        
        if (isBreakable)
        {
            HandleHits();
        }
    }



    void HandleHits()
    {
        timesHit++;
        maxHits = hitSprites.Length + 1; 
        if (timesHit >= maxHits)
        {
            breakableCount--;
            Debug.Log(breakableCount);
            levelManager.BrickDestroyed();
            Destroy(gameObject);
        }
        else
        {
            LoadSprites();
        }
    }



    void LoadSprites()
    {
        int spriteIndex = timesHit - 1;
        if (hitSprites[spriteIndex])
        {
            this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
        }
    }

    // TODO Remove this method once we can actually win!


    void SimulateWin()
    {
        levelManager.LoadNextLevel();
    }
}

Please help me, here.

Hi again,

Ok, couple of things…

Firstly, Application.loadedLevel is obsolete, so you would want to stop using that.

You can get the build index of the current scene by using;

    SceneManager.GetActiveScene().buildIndex;

Secondly, can you pop a screenshot up of your scenes in the build settings please.

1 Like

SceneManager.GetActiveScene().buildIndex will return an integer… you can use this in place of the Application.loadedLevel in your existing code, e.g.

public void LoadNextLevel()
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
}
1 Like

That is giving me a win screen after a level is over.
It has to give me a win screen only after i complete all the 3 levels.
Code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class LevelManager : MonoBehaviour {

	public void LoadLevel(string name)
    {
        Debug.Log("load the level requested for : " + name);
        Brick.breakableCount = 0;
        SceneManager.LoadScene(name);
    }

    public void QuitRequest()
    {
        Debug.Log("i want to quit!");
        Application.Quit();
    }

    public void LoadNextLevel()
    {
        Brick.breakableCount = 0;
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    public void BrickDestroyed()
    {
        if(Brick.breakableCount <= 0)
        {
            SceneManager.LoadScene("win");
        }
       
    }
}

Build settings :

Thanks for the screenshot of the build settings, I wanted to check that the win scene wasn’t in the wrong order.

Looking at your LevelManager code however…

If you look at the BrickDestroyed() method you’ll note how, if all of the bricks have been destroyed, you load the “win” scene.

This method is called from your HandleHits() method in the Brick class.

What is happening is that your first level is played, all the bricks are destroyed, thus the condition is true and you load the win scene.

What you want instead is this;

    /// <summary>
    /// Determines whether the next level should be loaded
    /// </summary>
    public void BrickDestroyed()
    {
        if (Brick.breakableCount <= 0)
        {
            LoadNextLevel();
        }
    }

This will then increment the build index by 1 and load the next playable level if there are any more to play, if not, you will be taken the win scene.

Hope this helps.

p.s.
For the code formatting it is three of these characters ` not three of these . :slight_smile:

Yes it worked !
Thank you so much Rob. :slight_smile:

1 Like

You are more than welcome Soumith, I am glad you have it working and can progress with the rest of the course :slight_smile:

1 Like

Privacy & Terms