invoke() 函数统一

编程入门 行业动态 更新时间:2024-10-27 06:32:12
本文介绍了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() 函数统一

本文发布于:2023-06-10 18:32:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/616436.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   invoke

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!