HOME GAMES MISC

How to switch scenes in unity

First, Scenes that you want to use need to be added to the scene list.
Its under: File → Build Profiles → Open Scene List

Option 1:

public void onButtonPressed()
{
SceneManager.LoadScene(“Level1”);
}

This relies on the name of the scene file, and works perfectly fine for small projects.

Option 2:

public void onButtonPressed()
{
SceneManager.LoadScene(1);
}

This relies on the index the scene has in the scene list, and works perfectly fine for small projects.

Option 3:

First, open your scenes .meta file, and grad the GUID. This is a unique identifier that never changes (unless you delete the scene).

private string guidLevel1 = “guidOfyourScene”;

public void onButtonPressed()
{
private string scenepath = AssetDatabase.GUIDToAssetPath(guidLevel1);
SceneManager.LoadScene(scenepath);
}

This solution is usefull for big projects, or if you are a person that renames or rearrages stuff often.
Because the GUID never changes, this code will always open the correct scene.
Moving it to a different folder, raneming it, and changing its index in teh scene list all have no effect on this method.


Links:

Bluesky Icon My Bluesky | Twitch Icon My Twitch | Bat Icon My GFs page ♥