LoadLevel does not work as explained

I did everything as explained, but this just does not work:

public void LoadLevel(string name){
	Debug.Log ("Level load requested for: "+name);
	Application.LoadLevel(name);
}

I still get the error: couldn’t be loaded because it has not been added to the build settings
But it has been added of course!

I googled it and i found that i should put the build sequence number instead of name. I tried and it worked!
Just to be clear, it worked like this:

public void LoadLevel(string name){
	Debug.Log ("Level load requested for: "+name);
	Application.LoadLevel(1);
}

Why??

Why is it working with name in the video lesson?
Where is it getting the level name that way?
When printing name this is what we get: UnityEngine.Debug:Log(Object)
Where is the level name there??

Thanks.

Hello @LordEstraven,

A little bit of information about Application.LoadLevel().

Load() is a method, in this case it is an overloaded method, meaning that it has more than one signature. A signature is effectively what can be passed into the method via parameters.

So, you actually have two choices;

Application.LoadLevel(string name) - using this signature you can pass in the name of the scene to load.

Application.LoadLevel(int index) - using this signature you can pass in the index of the scene of load.

In the case of the name of the level, this is the name of your scene. So, if you have a scene called “Win” and you want to load that scene, you would use Application.LoadLevel("Win").

Note: It is important that you enter the name of the scene exactly as it is named in your project.

Equally, if this scene was index number 4 in your build settings, you could use Application.LoadLevel(4).


See also;

1 Like

In the lesson he is using just ‘name’, literally, the argument name passed to the function, no scene name, no nothing. I can’t understand why it is working for him.

1 Like

This would be because the name of the level is being passed in to the method through the parameter.

Let me explain a little further with a better example this time :slight_smile:


using UnityEngine;

public class LevelManager : MonoBehaviour {

    public void LoadLevel(string name) {

        Debug.Log("LoadLevel Request for Level : " + name);
        Application.LoadLevel(name);
    } 
}

So, in the above example, name is a parameter for the method and it is of type string, meaning we can only pass a value in to it which is of the same type - a string.

When we call this method from another piece of code, we would pass the value for this variable in, lets do that below;

void Start() {

    LevelManager.LoadLevel("Win");
}

In the above example I state that the name of the level I want to load is Win, because it is of type string I have to place the value inside double quotation marks.

So, we pass "Win" to LoadLevel(string name), the parameter name now equals "Win".

We can access this parameter within that code block, as you can see from the Debug.Log() statement, and, when we call Application.LoadLevel(name).

Does this make more sense?

Does make perfect sense of course, I know how the thing works, but it’s not
what is being shown in the video lesson

1 Like

Could you give me the lecture number and the approximate time from the video where you are experiencing this issue please @LordEstraven.


Section 4, Lecture 49 - How to Load Scenes and Quit

1:30 (approx) - adding Application.Load() to the LevelManager

3:10 (approx) - build settings - adding the scenes

What isn’t covered by this point in this lecture is how the level name “Game” is being passed into the LevelManager and to the LoadLevel() method, that’s because it was covered in the previous lecture (number 48), at approximately 5:48. The previous lecture being the one where you wired the scripts to the buttons.

The functionality of my examples above is the same as this for all intents and purposes. Instead of calling it from another script (by typing the code yourself) - you have added your method LoadLevel() to the OnClick() event for the Start button. By doing so the Inspector than provides you with a mechanism to pass in the parameters. In this case there was only one, hence the one text box. “Game” is then entered as a string. Double quotation marks are not required in this approach as Unity will wrap that value up for you.

Hope this helps :slight_smile:

3 Likes

Holy cow…
Feeling stupid now.
Thank you so much.

1 Like

No worries, it’s just two different ways of doing the same thing. All part of the learning curve, nothing to feel stupid about :slight_smile:

Any other queries / problems let us know :slight_smile:

I was right there with you this afternoon, with the exact same issue :confused: I hadn’t rewound the lessons far enough to find this and I saw the number working as well…

1 Like

I wouldn’t worry about feeling stupid, I feel stupid trying to decipher all this.

1 Like

This was very helpfull for me too! I have been wondering a hour here with this same one little mistake I didn’t see done in the video :slight_smile: Thank you!

SOLVED :slight_smile:

1 Like

I am glad that my reply, and of course the original question by @LordEstraven, continues to be useful to others :slight_smile: