Problem with level changes

I just finished Lecture 88 and everything is running just fine, sort of. I am using Unity 4.6.9f1 and did not jump to Unity 5 yet. I followed everything to the exact instructions of the course, with the exception of one: I put a load screen in between levels. The code attached to the load screen is as follows:

// Written By Eric Phillips
// Phillips Game Design
// phillipsgamedesign@gmail.com

using UnityEngine;
using System.Collections;

public class Load : MonoBehaviour {
    
    public int level = 1;    //Level to open after splash
    public float setTime = 3.0f;    //Duration before loading next level
    
    float timer;     // Use this for initialization
    
    void Start () {
        timer = 0.0f; //Initializes Timer to 0
    }
    
    // Update is called once per frame
    void Update () {
        timer += Time.deltaTime; //Adds Time.deltaTime to timer each update
        if (timer > setTime)
        {
            Application.LoadLevel(level); //Loads Level at index
        }
    }
}

That is the only difference. So here is my problem:

I go to my start Menu, start the game, and play fine. If I play without losing, then everything loads fine for the next level. If I lose, go back to the start menu and start again… The level does not change even when all the blocks are clear. Anyone have an idea?

I have went over the start menu scene, the LoseCollider script, and the LevelManager script and still cannot find the issue. Somewhere I think I am losing the Breakable count, but I cannot tell why or where. As stated before, if I play through everything loads just fine. If I Lose once and go back through the menu, it does not get the breakable count back.

Hi Eric,

Have you tried using a Debug.Log statement to output the value of BreakableBlocks when you get to the lose screen?

Good Idea! I set a debug log up in the LevelManager, LoseCollider, and my Load script. Somewhere, it is adding to the number on load. Started with an output of 18, and as I continued the loop through the main menu it got up to 55. So something is adding to the brick count between the ball hitting the collider, going to the lose screen, then to the start menu, and back into the game. Therefore, if it is seeing that many more bricks, then there can never be a win condition.

I figured it out… It is not resetting the brick count numbers. It holds them in memory and adds to them on the level load. I’ll have to dig into this more :slight_smile:

With your “loading” scene in between levels, could you be loading a scene with blocks, thus incrementing the count, and then loading the “loading” scene immediately afterwards? It would give the effect of incrementing the count and you wouldn’t see the playable level because the “loading” scene loads etc. Just a thought.

In the original code the methods which loaded a new scene set the breakableCount to zero if I remember correctly. You could try that too, although if you are loading more scenes than neeses that wouldn’t be ideal and may be better to resolve first.

You might want to check the Build Settings to see what order your scenes are in as you are loading by scene build index etc

Working on a theory right now and it seems to be working. Since it seems my issues start with losing, I am resetting the breakable count inside the LoseCollider. I’ll post the results on that soon!

That was it! All I had to do was reset the breakable count within the lose collider and it runs smoothly now. I will go through later and see exactly why it was holding it in memory, I realize this is just a band-aid.

1 Like

A static in the LevelManager perhaps?

Nothing static there, I checked. Here is my LevelManager script:

using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {

    public void LoadLevel(string name) {
        Debug.Log("Level load requested for " + name);
        Application.LoadLevel(name);
        Debug.Log(Brick.breakableCount + " LevelManager");
    }
    
    public void QuitRequest() {
        Debug.Log("I want to quit!");
        Application.Quit();
    }
    
    public void LoadNextLevel() {
        Application.LoadLevel(Application.loadedLevel + 1);
    }
    
    public void BrickDestroyed(){
        if (Brick.breakableCount <= 0) {
            LoadNextLevel();
        }
    }
}
1 Like

I think you not having the breakable block count was the issue, in the course Ben has that set in the LoadNextLevel() methods.

Might be worth considering, if you haven’t already, creating a method for initialising the game, e.g. when the game runs for the first time, what values have to all be cleared/reset. When you click on the “Play Again” option, make sure that that method is called before loading the first playable scene.

Obviously having it on the lose collider will work too but it may be one of those less obvious things to spot should you come back to the code in say 6 months (e.g. a bit odd to set the game readiness state at the end of the game).

:slight_smile:

I do have the breakable count set, in the Brick.cs file just like the videos show. If it has been set in the LoadNextLevel(), then that is done after Lecture 88. I have followed all the videos exactly to this point. You are right though, setting it in the LoadNextLevel() makes more sense and I will try that.

Just got done trying that idea and it still reverted back to holding brick count in memory. Only way I have gotten it to work correctly is to put it in the LoseCollider so far. I will work on it :slight_smile:

hmm… what else is in the LoseCollider code Eric? Can you pop a post up of it…

This is my LoseCollider as it stands right now:

using UnityEngine;
using System.Collections;

public class LoseCollider : MonoBehaviour {

    private LevelManager levelManager;
    
    void OnTriggerEnter2D (Collider2D trigger){
        
        //Application.LoadLevel("Lose");
        levelManager = GameObject.FindObjectOfType<LevelManager>();
        Brick.breakableCount = 0;
        levelManager.LoadLevel("Lose");
        Debug.Log(Brick.breakableCount + " Lose Collider");
    }
    
    //void OnCollisionEnter2D (Collision2D collision) {
    //    print ("Collision");
    //}
}

So, looking at that;

Brick.breakableCount = 0;

should be able to go into the two LevelManager methods, LoadLevel and LoadNextLevel, it would need to be placed before you actually call Application.LoadLevel in both cases obviously.

e.g;

public void LoadLevel(string name) {
    Debug.Log("Level load requested for " + name);
    Brick.breakableCount = 0;
    Application.LoadLevel(name);
    Debug.Log(Brick.breakableCount + " LevelManager");
}

and

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

…and then if you comment out

Brick.breakableCount = 0;

from your LoseCollider, you should end up with exactly the same result as you have at the moment, rather than the game just carrying on.

Updated Sun Oct 30 2016 18:59

Of course, you have your Load class also, which also contains an Application.Load method call in it too, if you plan to keep this here, I’d still the Brick.breakableCount = 0 a line above that also… alternatively, you could perhaps make a call into LevelManager which may make more sense, as its your object managing the levels :slight_smile:

Seems like if I had just watched a couple more videos it would have been addressed. This issue is discussed in Lecture 91 :slight_smile:

1 Like

A post was split to a new topic: Problem loading levels after losing