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??
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).
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.
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
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).
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.
I was right there with you this afternoon, with the exact same issue I hadn’t rewound the lessons far enough to find this and I saw the number working as well…