How to change scene in Unity! In this tutorial we cover four different scenarios on how to switch scenes:

  1. Change Scene on Button Press
  2. Change Scene on Collision
  3. Switch scenes after time (delay)
  4. How to Change Level (scene) AND Keep Your Variables

❤️ Subscribe to Oxmond Tutorials. Stay ahead af the game!
https://bit.ly/SubscribeOxmondTutorials

Download the free DOTweeen tool here:
https://assetstore.unity.com/packages/tools/animation/dotween-hotween-v2-27676?aid=1100l4p9k

Free Assets from the Unity Assets Store:
https://assetstore.unity.com/lists/top-free-packages-13201?aid=1100l4p9k 

😷👕 Need a face mask / developer T-shirt? Drop by our shop and get a 20% DISCOUNT on your first purchase by using the discount code OXMONDSALE. Click here:
https://shop.oxmond.com/discount/OXMONDSALE



The button script:

/*

   .-------.                             .--.    .-------.     .--.            .--.     .--.        
   |       |--.--.--------.-----.-----.--|  |    |_     _|--.--|  |_.-----.----|__|---.-|  |-----.
   |   -   |_   _|        |  _  |     |  _  |      |   | |  |  |   _|  _  |   _|  |  _  |  |__ --|
   |_______|__.__|__|__|__|_____|__|__|_____|      |___| |_____|____|_____|__| |__|___._|__|_____|
   © 2019 OXMOND / www.oxmond.com 

*/

using UnityEngine;
using UnityEngine.SceneManagement;

public class BtnClick : MonoBehaviour
{
    public void BtnNewScene()
    {
        PlayerPrefs.SetInt("score", 2500);
        SceneManager.LoadScene("Scene 2");
    }

}


The Ball Collision Script:

/*

   .-------.                             .--.    .-------.     .--.            .--.     .--.        
   |       |--.--.--------.-----.-----.--|  |    |_     _|--.--|  |_.-----.----|__|---.-|  |-----.
   |   -   |_   _|        |  _  |     |  _  |      |   | |  |  |   _|  _  |   _|  |  _  |  |__ --|
   |_______|__.__|__|__|__|_____|__|__|_____|      |___| |_____|____|_____|__| |__|___._|__|_____|
   © 2019 OXMOND / www.oxmond.com 

*/

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

public class BallCollision : MonoBehaviour
{
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.name == "Ball Red") {
            SceneManager.LoadScene("Scene 2");
        }
    }
}


The Delay Script:

/*

   .-------.                             .--.    .-------.     .--.            .--.     .--.        
   |       |--.--.--------.-----.-----.--|  |    |_     _|--.--|  |_.-----.----|__|---.-|  |-----.
   |   -   |_   _|        |  _  |     |  _  |      |   | |  |  |   _|  _  |   _|  |  _  |  |__ --|
   |_______|__.__|__|__|__|_____|__|__|_____|      |___| |_____|____|_____|__| |__|___._|__|_____|
   © 2019 OXMOND / www.oxmond.com 

*/

using UnityEngine;
using DG.Tweening;
using UnityEngine.SceneManagement;

public class Delay : MonoBehaviour
{
    void Start()
    {
        DOVirtual.DelayedCall(3, GotoNextScene);
    }

    void GotoNextScene()
    {
        SceneManager.LoadScene("Scene 2");
    }
}



The Load Variable (Scene 2) Script:

/*

   .-------.                             .--.    .-------.     .--.            .--.     .--.        
   |       |--.--.--------.-----.-----.--|  |    |_     _|--.--|  |_.-----.----|__|---.-|  |-----.
   |   -   |_   _|        |  _  |     |  _  |      |   | |  |  |   _|  _  |   _|  |  _  |  |__ --|
   |_______|__.__|__|__|__|_____|__|__|_____|      |___| |_____|____|_____|__| |__|___._|__|_____|
   © 2019 OXMOND / www.oxmond.com 

*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Scene2 : MonoBehaviour
{

    public TextMeshPro scoreText;

    void Start()
    {
        scoreText.text = "Score: " + PlayerPrefs.GetInt("score").ToString();
    }

}