Trying to Invoke method: CollisionScript.ReloadLevel couldn't be called


Hey,VS told me "ReloadLevel"is declared but never used. Bur I did use it in the Invoke.
and in the unity :Trying to Invoke method: CollisionScript.ReloadLevel couldn’t be called.
I really dont know where is the problem. Can anybody save me?
here is the code:

using UnityEngine;
using UnityEngine.SceneManagement;
public class CollisionScript : MonoBehaviour
{

void OnCollisionEnter(Collision other)
{
   switch(other.gameObject.tag)
    {
        case "Start":
            Debug.Log("start");
            break;
        case "Finish":
            LoadNext();
            break;
        case "Fuel":
            Debug.Log("Fuel");
            break;
        default:
            Boom();
            break;
    }
   
    void ReloadLevel()
    {
        int Level = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(Level);
    }

    void Boom()
    {
        GetComponent<Mover>().enabled = false;
        Invoke(methodName: "ReloadLevel", time: 0.5f);
    }
    void LoadNext()
    {
        int Level = SceneManager.GetActiveScene().buildIndex;
        int NextLevel = Level + 1;
        if(NextLevel>SceneManager.sceneCountInBuildSettings)
        {
            NextLevel = 0;
        }
        SceneManager.LoadScene(NextLevel);
    }
   
}

}

1 Like

Hi @SunG615,

Welcome to our community! :slight_smile:

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

It seems as if your Reload method and other methods are wrapped in the code block of the OnCollisionEnter method. Please check the position of the curly brackets.


See also:

3 Likes

Thank you! Actually I am compared more than 1 hour yesterday : ( But my code works now ! thank you again!

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

Privacy & Terms