Adding Invoke breaks the Reloading mechanic

I followed the Video and after he added Invoke to delay the reloading mechanic I repeated the code he did and then it didn’t work anymore. If the Rocket hits an object which would make it reload it doesn’t work anymore. What in my code breaks the Reloading mechanic? I also got an Error Message in Unity after implementing Invoke: Assets\Scripts\CollisionHandler.cs(35,10): warning CS8321: The local function ‘Reloadlevel’ is declared but never used
Video showing what doesn’t work anymore

using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour
{
    void OnCollisionEnter(Collision other) 
    {
        switch (other.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("This thing is friendly");
                break;
            case "Finish":
                LoadNextLevel();
                break;
            case "Fuel":
                Debug.Log("You picked up some fuel");
                break;
            default:
                Debug.Log("You blew up");
                Invoke("Reloadlevel", 1f);
                Debug.Log("Test");
                break;
        }
    void LoadNextLevel()
        {
            int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
            int nextSceneIndex = currentSceneIndex + 1;
            if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
            {
                nextSceneIndex = 0;
            }
            SceneManager.LoadScene(nextSceneIndex);
        }
    void Reloadlevel()
        {
            int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
            SceneManager.LoadScene(currentSceneIndex);
        }
    }
}
1 Like

Hi, check your {} curly brackets! :wink:

For future reference, if you see that your curly brackets dont line up with the first line.
like this;

    void Reloadlevel()
        {
            int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
            SceneManager.LoadScene(currentSceneIndex);
        }

instead of this;

    void Reloadlevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex);
    }

Usually that means something is not right!

I found that you made the same mistake like me!

You put the Reload method and LoadNextLevel method in OnCollisionEnter method.

Just Separate Reload method and LoadNextLevel method from OnCollisionEnter method,and your bug will fix.

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

Privacy & Terms