Scene/Level changing problems
So I've been looking at some tutorials to figure out how to change levels/scenes in Unity. However, the tutorials were involving some older versions of Unity, not Unity 5.3. I've tried to figure out how to convert the information from the older API into the newer form, but I feel like that effort is futile. So, I'm here to ask the Unity community the following questions:
Am I scripting for the change in level correctly?
Why am I getting an error saying that on line 38, that there is an unexpected symbol "/", when it's expecting a "." If I am reading this correctly, it is referring to the first slash in the following line:
SceneManager.LoadScene(string /Volumes/...etc.);
I'm also going to provide my entire script to aid in your evaluation. Also, please bear in mind that this script is still a work in progress and there is nothing that is calling upon the "StartLevel" function yet. However, that shouldn't be a problem because my problem involves whether or not I'm actually writing the "StartLevel" function correctly
Also, before I provide my script, please let me know if you need more information:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class MenuScript : MonoBehaviour {
public Canvas quitMenu;
public Button startText;
public Button exitText;
void Start () {
quitMenu = quitMenu.GetComponent<Canvas> ();
startText = startText.GetComponent<Button> ();
exitText = exitText.GetComponent<Button> ();
quitMenu.enabled = false;
}
public void ExitPress()
{
quitMenu.enabled = true;
startText.enabled = false;
exitText.enabled = false;
}
public void NoPress()
{
quitMenu.enabled = false;
startText.enabled = true;
exitText.enabled = true;
}
public void StartLevel(){
SceneManager.LoadScene(string /Volumes/...etc.);
}
public void ExitGame()
{
Application.Quit ();
}
}
Answer by corn · Jan 14, 2016 at 05:06 PM
First of all, check the Scripting API page. That should be the first thing you do whenever you feel lost, because it is always up to date, unlike some tutorials.
As you know, line 38 is the issue :
SceneManager.LoadScene(string /Volumes/...etc.);
string /Volumes/...etc.
doesn't make any sense. I don't quite understand where you were going with that. However, you cannot use a / in a variable name. What you're actually doing there is declaring a string variable, but the variable name is incorrect, and even if it was, you cannot declare a variable when calling a function. Last but not least, even if you could, this string would not be initialized, so LoadScene would fail. So you really need to declare a string variable beforehand (and initialize it), then call LoadScene with this variable as a parameter.
There is two ways to load a scene with SceneManager.LoadScene : with the scene name, or with its index in the Build Settings. It is imperative that the scene is in the build settings (File > Build Settings > Add Current or drag and drop the scene you need) in order to be able to load it.
So let's just declare a string variable that will be our scene's name.
public class MenuScript : MonoBehaviour {
[SerializeField]
string m_LevelName = string.Empty;
...
}
That way, using SerializeField, you can change LevelName from the inspector, so there's no need to modify your script if you decide you want to load a different scene or if you change the scene's name.
Then, to load the scene :
public void StartLevel() {
SceneManager.LoadScene(m_LevelName);
}
And that's it !
I thought that I had to put the scene's path as the parameter. I figured it would be wrong, but the knowledge I've been getting from several different forums, websites, and tutorials gave me the impression that it would work (I'm such a silly person :P).
Anyway, so I got that to work, but whenever I call upon this function, I get a new error saying that the scene could not be loaded because it hasn't been added to the build settings or the AssetBundle hasn't been loaded. I know what it was referring to as the build settings, so I added the scene there, but it's still giving me the error, so I'm assu$$anonymous$$g that the problem lies within this AssetBundle thing it referred to. I have no idea what that is though, so maybe you could shine some light on this.
What is the name of your scene, and what is the string you're calling LoadScene with ? Have you tried using LoadScene with the scene's index rather than its name ? Also please post the entire error message.
The name of my scene is $$anonymous$$ain Level. The string I'm calling LoadScene with is (I'm assu$$anonymous$$g you're referring to the string within the script):
[SerializeField]
string m_$$anonymous$$ainLevel = string.Empty;
public void StartLevel(){
Scene$$anonymous$$anager.LoadScene (m_$$anonymous$$ainLevel);
}
In the inspector, I defined $$anonymous$$ain Level as the full path to the scene.
I can't post the entire error message, because it contains personal information, but I will definitely put down as much as I can:
Scene /Volumes/Groups...etc. (assume the entire scene's path has been put in) couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded. To add a scene to the Build settings use the menu File -> Build Settings.
Your answer
![](https://koobas.hobune.stream/wayback/20220612060213im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to modify many prefab objects in a script?, 0 Answers
change game character/controller when inside box - RFPSP 1 Answer
Jitter when moving around a circular path 0 Answers
Auto exit script 2 Answers
How can I fix my jumping script? 0 Answers