How to Change Scene in Unity
Tutorial, Unity
0
How to change scene in Unity! In this tutorial we cover four different scenarios on how to switch scenes:
- Change Scene on Button Press
- Change Scene on Collision
- Switch scenes after time (delay)
- 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(); } }
Tags In
2019
2D
between
button
C#
change
change level
change scene
change scene script
click
code
coding pirates
collision
delay
Hans Oxmond
how to
How to Change Scene in Unity
learn
lesson
level
load
manager
multiple
new
next
on button press
Oxmond Unity Tutorials
parameters
press
scene
scene manager
scene transition
script
switch
switch between scenes
switch scene
tip
transition
trigger
tutorial
Unity3d
video games