Invoke wont work

My code:
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
bool gameHasEnded = false;
public void EndGame ()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Invoke(“Restart”, 1f);
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}

It claims that “The local function ‘Restart’ is declared but not used.” I dont know why this is happening but it is happening.

USE CODE TAGS:

Formatted for you:

using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
    bool gameHasEnded = false;
    public void EndGame ()
    {
        if (gameHasEnded == false)
        {
            gameHasEnded = true;
            Invoke("Restart", 1f);
        }
        void Restart()
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }
}

I don’t even see how this code compiles… you have your Restart method defined inside your EndGame method.

Sorry to break it to you, but you script didnt work, but with the info you provided I managed to fix it.

Unity now supports C# 7 and up, which added the ability to create local methods.

Yeah… my script didn’t work because IT WAS YOUR SCRIPT.

I didn’t edit it… I just added the code tags so it became readable on the forum.

Do you not recognize your own damn code?

Aw, yeah I haven’t been on latest version of Unity using C#7 features… been version locked for a while now.

Back at OP, this would explain the problem though. a local function is not a member function of GameManager, where as Invoke only works on member functions.

I think someone accidentally used .Provoke() instead of .Invoke()… :slight_smile:

.Provoke() is part of the Anger Driven Development framework.

5 Likes