Unity 2017.4.1f1 - Making a button that loads the next scene

Same as the title.

The code provided in the video didn’t work, so after some sleuthing, I came up with this -
16
But that comes up with this error - Scene ‘Level load requested for: Game’ 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… But that doesn’t make any sense to me

Hello Duncan,

Welcome to the community :slight_smile:

You have a few things to deal with here.

Firstly, you are using a version of Unity which is more recent than that which the course was created using, there have been numerous API changes in that period of time. Thankfully, the ones that are relevant to the course are documented in a Google Doc which you can find linked under Resources on the Udemy website.

Specifically though, for this issue, you will need to do the following;

Add this line to the top of your script;

using UnityEngine.SceneManagement;

Next, within your LoadLevel method, replace this line;

Application.LoadLevel(name);

with the following;

SceneManager.LoadScene(name);

That takes care of the differences due to the API changes so far.

Your error about the Build Settings could be due to not specifying the name of the scene you wanted to load in your screenshot above, but we should check that you have your scene actually in your Build Settings, as that will also generate this error.

So, within Unity, goto File -> Build Settings

image

On the top you can see a pane which will display all of the scenes which you have added to be a part of your project;

If your scene is not currently listed, you can either use the Add Open Scenes button, or just drag it from the Assets folder into this pane.

Also, you can just copy/paste your code directly into the forum, this is often more useful than a screenshot as it allows those who offer to help you the ability to copy/paste from your code instead of having to type it all in themselves. Screenshots are however very useful when it comes to error messages or details from within the Unity editor itself.

Hope this helps. :slight_smile:


See also;

Hi Rob,

Thanks for the quick reply, I really appreciate it (I’m a complete novice).

I kind of managed to eventually solve the issue with this -

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelManager : MonoBehaviour {
    public void LoadLevel(string name){
        Application.LoadLevel("Game");
    }
}

Are there any glaring issue with this solution?

Thanks again!

1 Like

Hi Duncan,

You are more than welcome.

If we break down the desired functionality and then look at your code we can work through it.

In the Unity editor;

  • you have selected your button and it’s properties are displayed within the Inspector
  • within the OnClick events you have dragged your LevelManager across to the object field
  • doing so has populated the dropdown menu with a list of public methods, one of which will be your LoadLevel method
  • you have defined your methods signature to accept a string and have referred to this parameter as name
    public void LoadLevel(string name)
    
  • we want to make use of that parameter by passing in the name of the scene from the Inspector
  • there is a field underneath the dropdown menu for this, you may have already entered “Game” in that field

Using the code you have above, you will invariably still get an error due to the API changes. The LoadLevel method of the Application class has made obsolete.

By providing the name of the scene to load in you code would load the scene “Game” (with the changes from my earlier post), you would be limiting this method to only ever loading that one scene. If you then wanted to load another scene, perhaps called “Win” or “Lose” you would then need to construct additional methods for those specifically.

Where-ever possible you want to reduce the amount of redundant code, so if you want to make changes in the future you only need to change it in one place.

So, instead of your line;

Application.LoadLevel("Game");

…you want to make use of the parameter named “name” which you are passing in to that method, like this;

SceneManager.LoadScene(name);

The above uses the parameter given to the method, “name” could be holding anything as a string, as such, you could pass in “Win”, “Lose”, “Game” and that one method would work for all of them. Should you then ever want to make a change to it’s behaviour, you only need to change it in the one place.

Hope this helps :slight_smile:


See also;

That’s really helpful, thanks again!

1 Like

No problem, you are very welcome. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms