Using coroutine to change lvl

hey all I just wanted to check if this is good coding?

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

public class CollisionHandler : MonoBehaviour
{
    void OnCollisionEnter(Collision other) 
    {
        switch (other.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("you are touching friendly tag");
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartCrashSequence();
                break;
        }
    }

    private void StartSuccessSequence()
    {
        GetComponent<Movement>().enabled = false;
        GetComponent<AudioSource>().enabled = false;
        StartCoroutine(WaitNextLevel());
    }

    void StartCrashSequence()
    {
        GetComponent<Movement>().enabled = false;
        GetComponent<AudioSource>().enabled = false;
        StartCoroutine(WaitReloadLevel());
    }
    
    IEnumerator WaitReloadLevel()
    {
        yield return new WaitForSeconds(1);
       
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;//starting level
        
        SceneManager.LoadScene(currentSceneIndex);
    }

     IEnumerator WaitNextLevel()
    {
        yield return new WaitForSeconds(1);
        
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex +1;
        
        if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
        {
            nextSceneIndex = 0;
        }
        SceneManager.LoadScene(nextSceneIndex);
    }
    
}


iā€™m using coroutine to delay the nextlevel and when you crash to reset the level with a 1 second delay.

I did some programming in the past but never with unity.

should the coroutine be in another class or would this be a good place to put it?

This is fine

1 Like

Thank you :smiley:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms