How do I load the next scene in unity when my player hits a 2d sprite?
This is my current code –
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class Loader : MonoBehaviour { public Animator sceneChange; private Coroutine _loadInProgress; void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("End")) { Debug.Log("END"); LoadNextLevel(); } } public void LoadNextLevel() { if (_loadInProgress == null) { int nextLevel = SceneManager.GetActiveScene().buildIndex + 1; _loadInProgress = StartCoroutine(LoadLevel(nextLevel)); } } IEnumerator LoadLevel(int LevelIndex) { sceneChange.SetTrigger("Start"); yield return new WaitForSeconds(1); SceneManager.LoadSceneAsync(LevelIndex); _loadInProgress = null; } }
Where do I put this code so that when I collide I load the next level?