After adding Invoke to the load next level method; once I complete the last stage instead of looping back to the first, I receive that error.
I saw here the same issue but the answer seems limited? As in if I wanted to create another level I would have to change that code to be if (currentSceneIndex <= 2), etc?
As far as the code goes, its the same except for some names and I used the Invoke(nameof(),) way instead of using a string, but I have it below if somebody can spot something I missed. Rick doesn’t seem to have any issue at all.
using System;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.SceneManagement;public class CollisionHandler : MonoBehaviour
{
[SerializeField]float delayInSeconds = 0f;void OnCollisionEnter(Collision collision) { switch (collision.gameObject.tag) { case "Friendly": Debug.Log("You're safe for now!"); break; case "Finish": StartSuccessSequence(); break; case "Fuel": Debug.Log("You've picked up more fuel."); break; default: StartCrashSequence(); break; } } void StartSuccessSequence() { GetComponent<Movement>().enabled = false; Invoke(nameof(NextLevel), delayInSeconds); } void StartCrashSequence() { //todo add SFX //todo add particles GetComponent<Movement>().enabled = false; Invoke(nameof(ReloadLevel), delayInSeconds); } void ReloadLevel() { int currentSceneIndex = SceneManager.GetActiveScene().buildIndex; SceneManager.LoadScene(currentSceneIndex); } void NextLevel() { int currentSceneIndex = SceneManager.GetActiveScene().buildIndex; int nextSceneIndex = currentSceneIndex + 1; if (nextSceneIndex == SceneManager.sceneCountInBuildSettings) { nextSceneIndex = 0; } SceneManager.LoadScene(currentSceneIndex + 1); }
}