首页 > 解决方案 > 从 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 脚本

标签: unity3d

解决方案


您可以通过将其保持为静态来触发该 bool startGame,即 public static bool startGame。这样您就可以直接使用 LevelManager 触发。


推荐阅读