unity3d - 从 Unity 中的另一个脚本访问 public bool
问题描述
我正在寻找从另一个脚本触发 LevelManager 脚本中的公共布尔设置。
因此,当脚本 B 发生某些事情时,脚本 A 中的布尔值会切换为 true。
下面是 2 个脚本,其中 levelRestart.cs 是一个触发器,当玩家击中时,它需要访问 LevelManager 脚本中的公共 bool 并说startGame
是真的。
有人可以帮忙吗?
级别管理器.cs
public bool startGame = false;
void Update ()
{
if (startGame == true)
{
SpawnKonyaku();
startGame = false;
}
}
LevelRestart.cs
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
levelManager.startGame = false; //<- This is where, i need help
}
}
触发公共 bool startGame 为 true 的 RestartLevel 脚本
解决方案
您可以通过将其保持为静态来触发该 bool startGame,即 public static bool startGame。这样您就可以直接使用 LevelManager 触发。
推荐阅读
- jsf - 如何在jsf中使用selectOneMenu,对象的值不是String?
- c# - 将 OpenGL 4.5 代码降级到 OpenGL 3.0
- javascript - 如何使用reduce删除对象数组中的空值?
- android - 在同一应用程序(Android)中为移动/桌面和电视上传多个 APK。Google Play 开发者控制台无法正常工作
- .net - 将 utc 格式 DateTime 字符串转换为日期时间时出现异常
- c++ - 使用 C++ 写入 JSON 文件?
- computer-vision - 如何将 LSTM 输出反馈到 pytorch 数据加载器中的输入
- python - PDFMiner Version diffs?Getting UnsupportedOperation: seek
- javascript - 将 onmount json 传递给变量
- xcode - 导航控制器的视图控制器按钮将我带到全新的窗口,而不是下一页。为什么会这样?