Hi Vivek,
So, the files above and the copy/paste of the scripts, this is what you currently have, correct?
Also, you mentioned that you have tried to have a message appear after each level is completed, I don’t see any code in the above that looks like it might do that, have you taken it back out?
What errors are you currently receiving?
I would suggest that we work through these first, get a stable version of the game working at this point before trying to add the additional feature.
If you could answer the above and in the mean time I will take a look at all the download files as promised.
Updated Mon Jan 08 2018 23:00
Observations from scripts pasted above
Brick.cs
This line is unnecessary;
private SceneManager sceneManager;
The LoadScene
method of the SceneManager class is static, as such you do not need an instance of the SceneManager class to use it, you can just reference it via the class, as you are doing further down in your code in the Start
method. Note that SceneManager is capitalised, the name of the class, not sceneManager, your variable.
LevelManager.cs
The BrickDestroyed
method currently doesn’t do anything, as such, you will not progress to another level in the game, I’m not certain if you are aware of this, or if this is how far you have got in the course so far. I would suggest at the very least putting a Debug.Log
statement in there so that something is output to the console when the method is called and your condition is met;
public void BrickDestroyed()
{
if (Brick.breakableCount <= 0)
{
Debug.Log("Brick.breakableCount <= 0"); // add this line, just for now, for debugging purposes
}
}
LoseColllider.cs
The class name has an extra l character in it’s name, not a massive issue, as long as the filename is spelled the same way too.
Again, the following line is unnecessary and can be removed;
private SceneManager sceneManager;
You have both OnTriggerEnter2D
and OnCollisionEnter2D
. The latter is unnecessary and can be removed.
MusicPlayer.cs
You have misspelled the Awake
method (Aswake), as such this will never be called by Unity, as it is unrecognised, remove the s in the method name.
General
You have a bit of a lack of consistency in some of the naming of variables and the placement of braces/brackets. Whilst this won’t prevent your code from running, it’s not really best practice and will invariably only make it harder for yourself to debug when things go wrong.
As a very loose set of a guidelines for now, I would recommend you ensure that the following use Pascal case (PascalCase);
- Class names - example LevelManager
- Method names - example LoadScene
and the following use camel case (camelCase)
- variable names - example - autoPlay
- parameters names - example - sceneName
When it comes to naming variables, properties, methods, parameters for methods, generally anything - give some consideration to what it actually is. For example, the method MySwitchScenes
doesn’t really make a lot of sense, less so because it also has a parameter of the same name being passed into it (in PascalCase!).
If you think about exactly what this method is for, it’s simply to load another scene, so the term “My” is redundant. The terms “Switch” and “Scenes” less so, but still perhaps not as clear as they could be. I think in the course this method was called “LoadLevel”, that’s fairly simplistic, makes sense and everyone would know what it was for. Equally, you could perhaps call it “LoadScene”, just the actual SceneManager class does.
The parameter being passed in is the name of the scene to load, so why not simplify this and name it accordingly, levelName or sceneName.
With regards to the brackets / braces, at the moment you have a bit of a mixture between trailing and block style, e.g;
private void Start() {
}
and
private void Start()
{
}
Again, this wont make any difference from the perspective of the compiler, but when it comes to looking through your code for debugging purposes it is less easy on the eye. If you have been caught out at all by a missing closing brace for example, you may find it easier initially to adopt the block style approach;
private void Start()
{
if(missingClosingBrace == true)
{
Debug.Log("Try the block style approach.");
}
}
As you can see from the above, the brace line up with each either vertically, for both the method and the if
statement. This can make it easier to read and also easier to spot missing brackets and braces.
These may seem like fairly minor/petty points, but when you get into bigger projects, the last thing you want is to be spending a lot of time trying to work out all of the different naming conventions, and meanings of things instead of just looking for the problem and fixing it.
Ok, now I will go and grab those files!