Using Unity Scene Manager

Block title

The Unity Scene Manager was introduced in Unity 5.3 and changes the way that scenes are loaded in the game. Many, if not all of the old Application.LoadLevel function has been deprecated in Unity 5.3 for the new Scene Manager code. This guide will help upgrade C# code we found that was deprecated during this update related to scene loading and getting information such as the current loaded scene. The good thing is that everything that you currently have in your game is still going to work until you can get around to updating the deprecated code. It will just give you some annoying warnings with notices like the following:

Assets/Scripts/someScript.cs(10,12): warning CS0618: `UnityEngine.Application.LoadLevel(string)' is obsolete: `Use SceneManager.LoadScene'
Assets/Scripts/someScript.cs(20,22): warning CS0618: `UnityEngine.Application.loadedLevelName' is obsolete: `Use SceneManager to determine what scenes have been loaded'

I had quite a time trying to find the right code to use to fix these issues, as the documentation is pretty lacking so far. I figured I would need the information again probably, so I figured it would be helpful for other Unity developers getting these warnings since updating to Unity 5.3 also.

Let's Clean Up Those Notices!

Add the Scene Manager Namespace

First we need to make sure any script files we are modifying to use the new Screen Manager functions have the proper using Directive namespace added. Add the following line of code to any C# scripts that you will be modifying to use Scene Manager.

using UnityEngine.SceneManagement;

Search and Replace Application.LoadLevel

Next we want to search and replace all instances of 

Application.LoadLevel

with the following code:

SceneManager.LoadScene

You should end up with something like the following code after upgrading to use with Scene Manager:

SceneManager.LoadScene("Level1");

Search and Replace SceneManager.LoadScene(Application.loadedLevel);

Because we have already done a search and replace on SceneManager.LoadScene, we can use that to find and replace any instances of where we used to use Application.LoadLevel(Application.loadedLevel); with the new Scene Manager way of calling that function. Find and replace

SceneManager.LoadScene(Application.loadedLevel);

with the following code:

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

Search and Replace Application.loadedLevelName

Finally we need to find and replace all the remaining Application.loadedLevelName functions with the new Scene Manager function. Find and replace

Application.loadedLevelName

with the following code:

SceneManager.GetActiveScene().name

Troubleshooting

I ran into a few issues that were mistakes on my end that I wanted to point out for others.

Don't Forget the Namespace!

The functions will not work unless you add the Scene Manager namespace at the beginning of your C# script file.

using UnityEngine.SceneManagement;

Block title

Signup for Dropbox and get 2GB free storage plus 500GB per referral