How to load a scene in Unity C#
Hello programmers, In this article, I will show how “How to load a scene in Unity C#”.
Before we start the building process, we need to know a few concepts. Let’s first discuss them one by one.
SceneManager
SceneManager in Unity manages the scenes at run-time.
To define the SceneManager, we need to add “using UnityEngine.SceneManagement” to the System NameSpace on the top.
using UnityEngine.SceneManagement;
LoadScene in Unity
LoadScene loads the scene by scene name or index value in build in the system.
Load scene by index value:
public int buildIndex; SceneManager.LoadScene(buildIndex);
Load Scene by scene name:
public string SceneName;
public void LoadScene()
{
SceneManager.LoadScene(SceneName);
}The below-given program shows “How to load a scene in Unity C#”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class loadLevel : MonoBehaviour
{
public int iLevelToLoad;
public string sLevelToLoad;
public bool userIntegerToLoadLevel = false;
void Start()
{
}
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
GameObject collisionGameObject = collision.gameObject;
if(collisionGameObject.name == "Player")
{
LoadScene();
}
}
void LoadScene()
{
if (userIntegerToLoadLevel)
{
SceneManager.LoadScene(iLevelToLoad);
}
else
{
SceneManager.LoadScene(sLevelToLoad);
}
}
}In the above code when the player collides with the gameObject it loads New Scene.
Output:
Scene 1
When the player touches the black object Load New Scene :
New Scene:



Leave a Reply