当我对我想使用的方法使用 Invoke 时,我不能再调用它了吗? [英] When i use Invoke to a method i want to use i can't call it anymore?

查看:32
本文介绍了当我对我想使用的方法使用 Invoke 时,我不能再调用它了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始通过观看视频、阅读网络文章等来学习自己编码,我认为边做边学更适合我,所以我开始通过 Brackeys 教程视频使用 Unity 制作游戏.

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

        }

    }

所以我的问题是当我将 invoke 与方法 Restart 一起使用时,所有调用都不起作用.警告 CS8321 本地函数 'Restart' 已声明但从未使用过"这就是我得到的错误.

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.

如果需要,我可以展示我在哪里以及如何使用 endGame 方法.感谢您提供任何形式的帮助.

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

推荐答案

如果 Void 位于另一个 Void 中,它就不会工作.

所以你需要改变:

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

        }

    }

}

致:

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

    }
}

这篇关于当我对我想使用的方法使用 Invoke 时,我不能再调用它了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆