Uploaded Game to Gamebucket but Next Scene Doesnt Load

Loading a new Scene in Unity can be very straightforward, requiring only a single line of code.

All the same,

Despite being simple to do, the best technique for loading new Levels will vary depending on how you lot want to do it.

For example, you may desire to display a loading screen, a progress bar, or testify tips or other information while the histrion waits for a Level to load.

Likewise, how you structure your game may touch on how you lot load and move between Scenes.

If your game is modest, you might only need a few different Scenes.

However, if you lot're making a larger game, you might need to split your game, and even each level, into many different Scenes.

There is no 1 size fits all, but understanding the different options that are available to you, and seeing examples of how to actually apply them, tin make it much easier to pick the right method of moving between Scenes for your game.

Which is exactly what you'll larn in this commodity.

What you'll find on this folio:

  • How to load a new Scene in Unity
  • Load the next Scene in order
  • Load Scene vs Load Scene Async
  • How to make a Loading Screen in Unity
  • How to brand a Loading Progress Bar
  • How to fade the Loading Screen
  • Example 1: The Prefab Method
  • Example 2: The Persistent Method (using Don't destroy on load)
  • Example 3: The Loading Scene Method
  • How to pass data between Scenes in Unity

Offset, let's start with the basic method of loading a new Scene in Unity

How to load a new Scene in Unity

To load whatsoever Scene from a script in Unity, outset, you'll need to make sure that the Scene you desire to load is included in the build.

Otherwise, if the Scene hasn't been added to the Build Settings, or the Nugget Bundle hasn't been loaded, none of this volition work and the Scene won't load.

To check, select File > Build Settings and, if you tin't encounter the Scene listed, just elevate it to the list of Scenes from the Project View.

Unity Build Settings Window

Remember to add together the Scene you lot desire to load to the build, otherwise, none of this will work.

Next, you'll need to add the Scene Management namespace to whatsoever script that you want to load Scenes from.

Simply add "using UnityEngine.SceneManagement;" with the other using directives.

Similar this:

          using Organization.Collections; using System.Collections.Generic; using UnityEngine;            using UnityEngine.SceneManagement;                  

This will let you to use functions from the SceneManager class.

Finally, to load a new Scene, telephone call the Load Scene function, passing in the proper noun of the Scene y'all'd like to load.

Similar this:

          SceneManager.LoadScene("DemoScene");        

Alternatively, you can utilise the Scene's alphabetize instead.

Like this:

          SceneManager.LoadScene(ane);        

If you don't know the index of the Scene you want to load, you tin can look it up by opening the Build Settings. You can as well reorder Scenes to modify their index number.

Loading by a Scene's alphabetize tin can be particularly useful if you lot want to load Scenes in order; For case, if you want the role player to go on to the next Scene in the list at the end of each level.

If it suits the construction of your game, loading the next Scene instead of a specific Scene can be easier to manage, as you lot won't need to explicitly proper name the Scene to be loaded.

So how exercise you lot load the adjacent Scene on the listing?

How to load the next Scene in Unity

To load Scenes in order, i.due east. load whatever Scene is side by side on the list after the current Scene, all you demand to do is get the current Scene index and add 1.

Simple, right?

Here's what it looks like in scripting:

          using System.Collections; using Organization.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class LoadScene: MonoBehaviour {     public void LoadNextScene()     {         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);     } }        

All this does is get the index of the active Scene and increment it by one, which is ideal for advancing to the side by side Scene at the end of every level.

Go along in mind, however, that this just really works when your game structure allows for information technology (i.e. players are meant to experience every Level in order) and just when each Scene is in the correct society in the build list.

Because of this, yous'll demand to be conscientious to avoid accidentally reordering Scenes in the Build Settings.

Nevertheless…

This can likewise be a benefit.

While you will need to be careful to keep the Scenes in the order you desire them, it also means yous tin can move Scenes to a different position very hands.

When using this method, changing the order of Scenes in your game is equally like shooting fish in a barrel as reordering the build list.

Load Scene vs Load Scene Async

In that location are two unlike methods bachelor for Loading a scene.

Load Scene and Load Scene Async.

And so what's the difference?

Load Scene, as used in the before example, loads the Scene directly with loading taking identify during the next frame:

          SceneManager.LoadScene(sceneToLoad);        

While the Scene loads, the game volition freeze.

Music and audio will keep to play but the game will be unresponsive until the load has finished.

Depending on the corporeality of time that's required to load the next Scene, this pause can give the impression that the game has crashed, unless you hide the suspension with a Loading Screen (more on that later).

However, information technology's besides possible to load the side by side Scene in the groundwork, asynchronously, while notwithstanding assuasive the game to run, using Load Scene Async.

Load Scene Async works in a similar way to Load Scene, except the loading takes place as a background functioning and is spread over multiple frames:

          SceneManager.LoadSceneAsync(sceneToLoad);        

There are a few reasons that you lot might desire to practice this.

For example, you might want to keep the current Scene running until the new Scene is ready.

Or, if your loading screen includes animated elements, such as tooltips or a loading icon, these tin can only work when loading the Scene in the background.

Also, every bit an Asynchronous Operation, you have the choice to check when the loading has finished using AsyncOperation.isDone. Y'all tin even get the current progress of the load, which is useful when making a loading progress bar.

How to get the Load Scene Async progress

To access the progress of the load, cache a reference to the Asynchronous Operation when you call it.

Like this:

          AsynchronousOperation loadingOperation = SceneManager.LoadSceneAsync(sceneToLoad);        

You can then access the operation's properties.

For example the progress:

          bladder loadProgress = loadingOperation.progress;        

Which returns the progress of the load equally a float value betwixt 0 and one.

Or to check if loading is finished.

Like this:

          if(loadingOperation.isDone)     {         // Loading is finished !     }        

These additional properties are only available when using Load Scene Async.

Why does Unity still freeze and stutter when using Load Scene Async?

Load Scene Async, while described every bit a background functioning, is still a heavy task to perform.

You may observe that information technology will still crusade the game to stutter or even freeze while loading takes place.

Nonetheless…

If this is happening in your project, you might not actually have an issue.

Loading a new Scene in the editor will bring near Scenes, even very bones ones, to a consummate halt.

However, if you build the project beginning you'll find that the loading process, and speed, is significantly better than when previewing information technology in the editor.

So, if you lot're having bug with how smoothly your Scenes load, and you haven't tried edifice your project all the same, attempt running a finished build commencement.

This volition help you to get an thought of what the loading process is really like in your game.

How to make a Loading Screen in Unity

Regardless of which loading method you use, it's oft a good idea to apply a Loading Screen to disguise the loading procedure.

And while you lot may adopt to avoid Loading Screens altogether, every game needs to load something at some point and using one can brand the experience of transitioning between menus and levels much smoother.

So what should you add to your Loading Screen?

In its most basic form, you'll demand something to comprehend the screen with that tin can also be used as a groundwork for a loading message or other information.

For example, a UI image, set up to black and stretched to fill the screen, works well for this:

Unity Loading Screen UI Image

Add a UI Paradigm to deed as a background for your Loading Screen.

Better nevertheless, why not add a custom paradigm that sets upwardly the Level that'due south beingness loaded.

Here I've used an in-game paradigm of the Sun Temple asset that I've been using to examination with, with a faded expanse at the bottom to leave some space for messages.

Custom Loading Screen Image

Using a custom groundwork image tin assistance set up the scene for the Level that's being loaded.

Speaking of messages, there should exist something to indicate that loading is taking place.

Why?

The indicate of using a Loading Screen at all is to communicate that the game has non, in fact, crashed. Then it'south a good idea to include some sort of indicator that in that location is something going on behind the scenes.

This could exist a uncomplicated "Loading" bulletin or some kind of animated icon.

Loading Screen Spinner Unity

I added a Text object and a rotating Image to make this loading message.

While an animated icon tells you that the game is loading, a progress bar or a percentage display is a great way to show how much of the Level is loaded.

So how do you exercise that?

How to make a loading Progress Bar in Unity

Unity Loading Bar Example

Here's how to brand a loading Progress Bar in Unity.

i. Prepare a Slider to act equally a Progress Bar:

  • Create a UI Slider

  • Remove the handle

  • Set the left and right values in the Rect transform to zero on both the Fill Area object and the Fill object

Remove Slider Padding

2. Load the Scene Asynchronously

  • When loading the Scene use Load Scene Async, otherwise this won't work
  • Cache a reference to the Asynchronous Functioning when you telephone call information technology, like this:
          public string sceneToLoad; AsyncOperation loadingOperation;    void Commencement()     {         loadingOperation = SceneManager.LoadSceneAsync(sceneToLoad);     }        

iii. Update the Slider during the load

  • Get a reference to the Slider (remember to add together using UnityEngine.UI; to the script)
  • Get the loading progress, which is a float value between 0 and i (using AsyncOperation.progress)
  • Using Mathf.Clamp01, catechumen the progress to a value between 0 and 0.nine, like this:
          public cord sceneToLoad; AsyncOperation loadingOperation; Slider progressBar;  void Offset()      {         loadingOperation = SceneManager.LoadSceneAsync(sceneToLoad);     } void Update()     {         progressBar.value = Mathf.Clamp01(loadingOperation.progress / 0.9f);     }        

Why do y'all need to convert the progress to value betwixt 0 and 0.9f?

This is considering the loading performance'due south progress is but measured upward to 0.9, with the final 0.1 being used for activation (which won't be visible on the Slider).

Doing it this fashion ways the bar will fill up completely.

For a really practiced explanation on how this works, try Brackeys' video (Youtube).

Keeping the last value between 0 and ane makes it a footling easier to also convert the loading progress to a percentage, which is useful if yous'd like to display a percentage value also.

Brandish the load progress as a percentage

To brandish the load progress as a per centum, but like before, when creating the Progress Bar, take the Async Operation Progress value and catechumen the value to a float between 0 and 0.9:

          float progressValue = Mathf.Clamp01(loadingOperation.progress / 0.9f);        

Then, to display it every bit a percentage, simply round the value, multiplied by 100, to the nearest integer.

Like this:

          public Cord sceneToLoad; AsyncOperation loadingOperation; public Text percentLoaded;     void Start()     {         loadingOperation = SceneManager.LoadSceneAsync(sceneToLoad);     }     void Update()     {         bladder progressValue = Mathf.Clamp01(loadingOperation.progress / 0.9f);         percentLoaded.text = Mathf.Circular(progressValue * 100) + "%";     }        

How to fade the Loading Screen

Fading in the Loading Screen is a great mode to polish out the transitions between screens.

But, while single UI elements can be faded past setting the Alpha of their Sail Renderer, chances are your Loading Screen is made up of multiple elements.

Luckily, there's a uncomplicated solution.

A Canvas Group component makes information technology piece of cake to apply settings to a number of UI elements all at once.

Canvas Group Component Unity

Command multiple UI elements with a Canvas Group

For case, to create a Loading Screen you might apply multiple UI objects, such as a background image, some text and a slider.

Using a Canvas Group makes it easy to employ the aforementioned settings to all of those objects, and then long as they're Kid objects of the Parent Loading Screen.

To fade the screen, Lerp the Canvas Grouping Alpha value between 0 and ane in a Coroutine.

  • The correct way to Lerp in Unity (with Examples)
  • Coroutines in Unity (how and when to use them)

Like this:

          public CanvasGroup canvasGroup;     void Start()     {         StartCoroutine(FadeLoadingScreen(2));     }     IEnumerator FadeLoadingScreen(float duration)     {         bladder startValue = canvasGroup.blastoff;         float time = 0;         while (time < duration)         {             canvasGroup.alpha = Mathf.Lerp(startValue, 1, time / duration);             fourth dimension += Fourth dimension.deltaTime;             yield render null;         }         canvasGroup.alpha = 1;     }        

When fading during loading, just equally with other animation, you'll need to use Load Scene Async.

That is, if you desire loading to start at the same time as the fade.

However, if you're using the bones Load Scene method, and you still want to fade the screen, you lot tin can.

Just make sure to perform the fade before starting the load.

Make information technology interesting

One time y'all know how to build the unlike elements, it becomes easier to create a Loading Screen that's unique to your game.

And, personally, I think that it'due south worth taking extra time and try to create a Loading Screen that is engaging, nice to look at or that smoothes out the transitions between menus and levels in your game.

Why?

Considering loading is waiting and waiting, even in pocket-sized bursts, is rarely fun.

And, while it'southward inevitable, in many cases at least, to have to wait for some part of your game to load, that doesn't hateful it tin't be a pleasant experience, or informative or, particularly in catchy games, a chance to familiarise the player with some of the controls or concepts of the game.

So why not brand the near of it.

Calculation the Loading Screen to your game (iii examples)

Once you lot've designed a Loading Screen, it's fourth dimension to implement information technology.

And while there'southward no ane fashion to exercise this, in that location are a few options that may exist more or less suitable, depending on the how yous organise your game's structure.

Below are three different options for adding the Loading Screen to your Unity project.

Example 1: The Game Object method (using a Loading Screen Prefab)

This is, potentially, the simplest of the three methods in that information technology requires very lilliputian fix.

You lot merely create your Loading Screen as y'all want information technology, store it every bit a Prefab, and then add to any Scene that requires a Loading Screen.

Like this:

Using a Loading Screen Prefab makes it easier to manage the object from 1 identify.

          using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class LoadScene : MonoBehaviour {     public GameObject loadingScreen;     public string sceneToLoad;     public void StartGame()     {         loadingScreen.SetActive(true);         SceneManager.LoadScene(sceneToLoad);     } }        

The benefit of this method is that it's very unproblematic and easy to manage, and whatsoever changes to the Loading Screen can be made to the Prefab object.

However, you lot'll nevertheless need to add together the Loading Screen object, and get a reference to it, in every Scene that uses it, which could go difficult to manage.

And while this isn't necessarily an sectional problem (after all there will exist other objects that are common to every Scene too, such every bit the player) you may notice it easier to manage only a single Loading Scene object in one location.

Such as in the Persistent Method.

Instance two: The Persistent Method (using Don't Destroy on Load)

This method works in almost the same way as the Game Object method, with one difference.

Instead of using multiple Loading Screen objects (one for each Scene) that all derive from a Prefab, this method uses a single object that is reused for every Scene.

This is made possible by using Don't Destroy on Load, a function in Unity that prevents an object from existence Destroyed when a new Scene is loaded, as is the default.

To use it, call the Don't Destroy on Load function, passing in the Game Object you want to carry over to the adjacent Scene.

In this instance, pass in the Loading Screen Object

Like this:

          public void Awake()      {      DontDestroyOnLoad(gameObject);      }        

This is useful for creating more complex Loading Scene transitions that continue until after a Scene is loaded.

For example, this jigsaw loading transition from Banjo Kazooie: Nuts and Bolts overlaps both Scenes, starting before and ending after the switch. In Unity, using Don't Destroy on Load allows you to create like animations beyond two Scenes with just 1 Loading Screen object.

Banjo Kazooie Loading Screen

This loading sequence from Banjo Kazooie: Nuts and Bolts overlaps 2 Scenes. (Source)

In the example below I utilise the Persistent method to fade in a Loading Screen, earlier fading the same Loading Screen out once again once the new Scene is loaded.

          using Organisation.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class SceneLoader : MonoBehaviour {     public GameObject loadingScreen;     public string sceneToLoad;     public CanvasGroup canvasGroup;     public void Kickoff()     {         DontDestroyOnLoad(gameObject);     }     public void StartGame()     {         StartCoroutine(StartLoad());     }     IEnumerator StartLoad()     {         loadingScreen.SetActive(true);         yield return StartCoroutine(FadeLoadingScreen(i, 1));         AsyncOperation operation = SceneManager.LoadSceneAsync(sceneToLoad);         while (!performance.isDone)         {             yield return null;         }         yield return StartCoroutine(FadeLoadingScreen(0, 1));         loadingScreen.SetActive(false);     }     IEnumerator FadeLoadingScreen(bladder targetValue, float elapsing)     {         float startValue = canvasGroup.blastoff;         float time = 0;         while (time < duration)         {             canvasGroup.alpha = Mathf.Lerp(startValue, targetValue, time / elapsing);             time += Fourth dimension.deltaTime;             yield return null;         }         canvasGroup.alpha = targetValue;     } }        

Create a split up Canvass just for your Loading Screen

When using the Persistent method, you may wish to create your Loading Screen on its own Canvas.

The Sheet object stores and displays UI elements for your game. It's automatically created when y'all add a UI element and is the parent object for everything that you add to your menu, hud and, in this case, Loading Screen.

However, it'southward likewise possible to create Canvasses manually, that are dissever from others in the Scene.

And while creating your Loading Screen on its own Canvass isn't required for this method, it can make managing it easier.

This is because Don't Destroy on Load only works at the root level of an object, so it makes sense to split up the Loading Screen Canvas from other UI elements that y'all don't want to have through to other Scenes.

Separating the Loading Screen Canvas in this way means yous may finish upward using multiple Sheet objects in your Scene.

To make sure that your Loading Screen Canvas renders above other UI elements, ready its Sort Order college than other Canvas objects.

Managing Multiple Canvases in Unity

When using multiple canvases in Unity, the Canvas sort order decides which will be displayed on top.

Instance 3: The Loading Scene method (using static data)

The Loading Scene Method involves using a dedicated Scene to brandish the Loading Screen instead of an in-game object.

This method is great for building really complex Loading Screens, with game tips, rotating 3D models, animated visuals etc.

And while you can do all of that with an in-game object, it's much neater and easier to manage in a separate Scene.

Then how does it piece of work?

  1. Create a Scene, called "Loading" or something similar, and build information technology just like you would using any other method.
  2. Then, when yous want to load a new Scene, instead of directly Loading the target Scene, transition to the defended Loading Scene.
  3. Inside the Loading Scene, add a script to load the side by side level of the game as before long as the Loading Scene starts.

Using a Loading Scene in Unity

Creating one special Loading Scene can be an easy way to manage Loading Screen objects in Unity.

Unproblematic, right?

Except, in that location's a problem.

Assuming that your game is fabricated up of many different Levels, how will the Loading Scene script know which Scene to load side by side?

Luckily there's a simple method for passing data to a new Scene when you load it.

How to pass data and variables between Scenes in Unity

There are several options for saving data that can be reused at a after time. For example, Role player Prefs or by saving information to a file.

Yet, for the purpose of transferring data from one active Scene to the next, a Static Class, that's created solely for passing data when loading, is one of the more than appropriate options available.

This involves creating a public static grade with public static variables that tin be used to shop the data that you lot want to transfer between Scenes.

In this instance a static string variable tin exist used to store the proper name of the Scene that needs to be loaded side by side.

So, when the loading script in the Loading Scene launches, it can read the same value and load the required Scene.

This is helpful because the loading trigger doesn't need to get a reference to a Loading Screen object or the Loading Data course.

Because it's a publicly accessible static class, all you lot need to do is blazon the class proper noun. Any object can set the value directly for the Loading Screen script to read.

Hither's how to do it:

  1. Create a new C Sharp script inside your Project View (retrieve there will not be an example of this script in any Scene). Call it LoadingData or something like
  2. Remove the Commencement and Update functions, equally y'all won't need them
  3. Remove ": Monobehaviour"as static classes tin can't derive from Monobehaviour
  4. You can also remove the "using" directives from the top of the script, as y'all won't demand them
  5. Add the "static" keyword between public and form
  6. Finally, add a public static string variable called sceneToLoad or similar

Your script should now look something like this:

          public static course LoadingData  {     public static string sceneToLoad; }        

In the Loading Screen Scene, create a script on a Game Object to read the Loading Data and load the target Scene.

Like this:

          using UnityEngine; using UnityEngine.SceneManagement; public form LoadScene : MonoBehaviour {     void Starting time()     {         SceneManager.LoadSceneAsync(LoadingData.sceneToLoad);     } }        

Finally, when you want to trigger a Scene load from the game or from a menu, add the post-obit script, setting the string value to the name of the new Scene you lot want to load.

Like this:

          using UnityEngine; using UnityEngine.SceneManagement; public form SceneLoadTrigger : MonoBehaviour {     public cord targetScene;     public void LoadScene()     {         LoadingData.sceneToLoad = targetScene;         SceneManager.LoadScene("Loading");     } }        

This sets the scene name in the static Loading Data form and and then immediately loads the Loading Scene which, in turn, reads the scene name and loads the final target Scene.

Like this:

How to pass data between Scenes Unity

Using a Static Class makes it like shooting fish in a barrel to pass data from 1 Scene to another, such as the name of the next Scene.

What'southward more, this method can exist used to display additional data about the loading Scene.

For example, you could configure the Loading Screen to brandish a custom image or message based on which Scene was being loaded.

Now it's your plough

How are you lot loading Scenes in your game?

Do you take many Scenes to deal with, or only a few? And how are yous managing them?

Or maybe you've got a dandy tip for loading Scenes in Unity that other people would dear to know.

Whatever it is, let me know by leaving a comment below.

Get Game Evolution Tips, Straight to Your inbox

Go helpful tips & tricks and principal game evolution basics the easy way, with deep-dive tutorials and guides.

My favourite time-saving Unity assets

Rewired (the best input management system)

Rewired is an input direction asset that extends Unity's default input system, the Input Manager, calculation much needed improvements and support for modern devices. Put just, it'due south much more advanced than the default Input Director and more reliable than Unity's new Input System. When I tested both systems, I establish Rewired to be surprisingly easy to apply and fully featured, so I tin understand why everyone loves it.

DOTween Pro (should be built into Unity)

An asset and so useful, it should already be built into Unity. Except it'due south non. DOTween Pro is an animation and timing tool that allows you to breathing anything in Unity. You tin motility, fade, calibration, rotate without writing Coroutines or Lerp functions.

Easy Save (there's no reason not to use it)

Easy Save makes managing game saves and file serialization extremely piece of cake in Unity. So much and so that, for the time information technology would take to build a save system, vs the cost of buying Easy Save, I don't recommend making your own save system since Easy Salvage already exists.

leescigigive.blogspot.com

Source: https://gamedevbeginner.com/how-to-load-a-new-scene-in-unity-with-a-loading-screen/

0 Response to "Uploaded Game to Gamebucket but Next Scene Doesnt Load"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel