When i use Invoke to a method i want to use i can't call it anymore?

不羁岁月 提交于 2021-01-20 09:12:09

问题


I started to learn coding myself by watching videos, reading web articles etc. and i thought that learning by doing would suit better for me so i started to make a game with Unity through Brackeys tutorial videos.

using UnityEngine.SceneManagement;
using UnityEngine;

public class gameManager : MonoBehaviour
{
    bool hasGameEnded = false;

    public float restartDelay = 1f;
    public void endGame()
    {
        if (hasGameEnded == false)
        {
            
            hasGameEnded = true;
            Invoke("Restart", 2f);
        }

        void Restart()
        {

            SceneManager.LoadScene(SceneManager.GetActiveScene().name);

        }

    }

So my problem here is when i use invoke with the method Restart none of the callings works. "Warning CS8321 The local function 'Restart' is declared but never used" Thats the error i get.

If wanted i can show where and how i used the endGame method. Thanks for any kind of help.


回答1:


If the Void Was inside another Void it wont work.

So you need to change :

using UnityEngine.SceneManagement;
using UnityEngine;

public class gameManager : MonoBehaviour
{
    bool hasGameEnded = false;

    public float restartDelay = 1f;
    public void endGame()
    {
        if (hasGameEnded == false)
        {
            
            hasGameEnded = true;
            Invoke("Restart", 2f);
        }

        void Restart()
        {

            SceneManager.LoadScene(SceneManager.GetActiveScene().name);

        }

    }

}

To :

using UnityEngine.SceneManagement;
using UnityEngine;

public class gameManager : MonoBehaviour
{
    bool hasGameEnded = false;

    public float restartDelay = 1f;
    public void endGame()
    {
        if (hasGameEnded == false)
        {
            
            hasGameEnded = true;
            Invoke("Restart", 2f);
        }
    }

    void Restart()
    {

        SceneManager.LoadScene(SceneManager.GetActiveScene().name);

    }
}


来源:https://stackoverflow.com/questions/65526960/when-i-use-invoke-to-a-method-i-want-to-use-i-cant-call-it-anymore

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!