Change Scene On Button Click Using C# Scripts In Unity

Introduction

 
This article demonstrates how to make Unity change the scene on a button click using C# scripts.
 
Prerequisites
 
Unity Environment version 2018.4.19f1
 
Create a New Project
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Create panel in Unity
 
First, you have to open the Unity project. Create the Panel for your game.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
The panel will add text on the Scene view
 
Click on the "GameObject" menu in the menu bar. Select UI and pick the "Text" option.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
The canvas will be added to the button.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Import the Button icon.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Drag n Drop Exit button icon on the Scene view.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Drag n Drop a Previous and Next button icon on the Scene view.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Select the Scene1 on Click (Ctrl + D). Create a Duplicate Scene2.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Create the Scripts
 
Right-click on Assets. Select Create >> C# script
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Rename the Scripts as Exit.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Double click on the Exit script. Write the code as shown below:
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. public class Exit: MonoBehaviour {  
  5.     public void exitgame() {  
  6.         Debug.Log("exitgame");  
  7.         Application.Quit();  
  8.     }  
  9. }   
Save the Program
 
Drag and drop the Exit script onto the Exit button change Scene on a Button click.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Create the Scripts
 
Right-click on Assets. Select Create >> C# script
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Rename the Scripts as SceneChange.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Double click on the Scenechange script. Write the code as shown below:
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. using UnityEngine.SceneManagement;  
  5. public class SceneChanger: MonoBehaviour {  
  6.     public void Scene1() {  
  7.         SceneManager.LoadScene("Scene1");  
  8.     }  
  9.     public void Scene2() {  
  10.         SceneManager.LoadScene("Scene2");  
  11.     }  
  12.     public void Scene3() {  
  13.         SceneManager.LoadScene("Scene3");  
  14.     }  
  15. }   
Save the Program
 
Create an Empty Gameobject.
 
Select the Game Object >> Create Empty GameObject.
 
Rename the empty gameobject as a Scene change.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Drag and drop the Scenechange script onto the change Scene on Button click.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Click on the "File" menu in the menu bar. Select the build settings, switch the platform, and add an Open scene select.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
Click on the Play button. Select change Scene on Button click.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
For runtime, select the Button as the Scene1 Change On Click in Unity.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
For runtime, select the Button as the Scene2 Change On Click in Unity.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 
For runtime, select the Button as the Scene3 Change On Click in Unity.
 
Unity Change Scene On Button Click Using C# Scripts In Unity
 

Summary

 
I hope you understood how to make Unity change the scene on a button click using C# scripts.