Error Reloading First Scene After Adding Invoke?

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);
}

}

void NextLevel()
{
//you create an int that is equal to the CURRENT scene's build index number
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
//you create an int that is equal to the NEXT scene's build index number
    int nextSceneIndex = currentSceneIndex + 1;
//you check if the NEXT scene's build index is more than the total number of scenes 
//to prevent loading a scene that does not exist
    if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
    {
        nextSceneIndex = 0;
    }
//you load the CURRENT scene+1, ignoring the check you did above
//if you replace "currentSceneIndex + 1" by "nextSceneIndex" the error should be gone i guess.
    SceneManager.LoadScene(currentSceneIndex + 1);
}

Oh, so I was essentially nullifying the entire if statement by using the currentSceneIndex + 1 instead of the variable I created. That makes a lot of sense, thank you so much for a quick reply!

1 Like

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

Privacy & Terms