invoke() 函数统一 [英] invoke() Function in unity

查看:24
本文介绍了invoke() 函数统一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几乎是我第一次使用 C 锐利和统一.我正在尝试统一使用 invoke() 函数,但它给出了错误

Its almost my first time using C sharp and unity. I am trying to use invoke() function in unity but its giving the error

尝试调用方法:无法调用 EndGame.Restart1."

"Trying to Invoke method: EndGame.Restart1 couldn't be called."

public class EndGame : MonoBehaviour
{ 
    bool GameHasEnded = false;
    public float Timer = 1f;
    
    public void endgame() 
    {        
        if (!GameHasEnded) 
        {
            GameHasEnded = true;
            Debug.Log("GameOver");
            Invoke("Restart", Timer);
        }
   
        void Restart()
        { 
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }
}

推荐答案

  • 您正在尝试Invoke本地方法 嵌套在另一个方法endgame 中.

    • You are trying to Invoke a local method that is nested inside of another method endgame.

      Afaik MonoBehaviour.Invoke 只能在类级别调用方法.

      Afaik MonoBehaviour.Invoke can only call methods on class level.

      它也可能是这里或您的原始代码中的一个错字,但 Restart1 不存在,只有 Restart.为了避免基于名称的代码中的拼写错误,我将使用 nameof

      It also is either a typo here or in your original code but Restart1 doesn't exist only Restart. To avoid typos in name based code I would use nameof

      你的代码应该看起来像

      public class EndGame : MonoBehaviour
      {
          private bool GameHasEnded = false;
      
          // Timer is strange name for that
          // I would suggest "Delay"
          public float Delay = 1f;
      
          public void endgame()
          {         
              if (!GameHasEnded)
              {
                  GameHasEnded = true;
                  Debug.Log("GameOver");
      
                  // In general in order to avoid typos I would prefer to use "nameof"
                  Invoke(nameof(Restart), Delay);
              }
          }
      
          private void Restart()
          {       
              SceneManager.LoadScene(SceneManager.GetActiveScene().name);
          }
      }
      

      这篇关于invoke() 函数统一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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