Unity3d:脚本改变属性值,但什么也没发生

本文关键字:什么 脚本 改变 属性 Unity3d | 更新日期: 2023-09-27 18:11:48

我已经设置了一个类型为Mode的公共属性(一个enum)"mode"用于我的GameObject的GameManager Monobehaviour,但是有时候,特别是在回调时,给所述属性赋值并没有做任何事情:

例如:

public class GameManager : uLink.MonoBehaviour {
    public static GameManager instance;
    public bool dedicated = false;
    public Mode mode = Mode.MainMenu;
    public List<Player> players = new List<Player> ();
    public Player localPlayer;

    // Use this for initialization
    void Start ()
    {
        GameManager.instance = this;
    }
    #region Server
        #region Network
    void uLink_OnServerInitialized ()
    {
        Debug.Log ("Server successfully started on port " + uLink.Network.listenPort);
        new Authenticator ("127.0.0.1", 1234, "DB", "dbUser", "dbPassword");
        mode = Mode.ServerDone;
    }
    (...)
    public enum Mode
    {
        MainMenu, //
        ServerInit, //Server start
        ServerStarting, //Server is starting
        ServerDone, //Server done loading
        ClientInit, //Client starting up
        ClientConnected, //Client connected
        ClientAwaitAuth,
        ClientAuthed, //Client authenticated
        ClientAuthFailed,
        Client //Client mode
    }
}

(这发生在使用Unity3D的网络和uLink的网络时)

回调发生,日志被放置在控制台上,验证器单例也被创建,但模式没有改变。我试过调试并在mode = Mode.ServerDone;上放置一个断点,但它只是说"值未加载"

如果我使用编辑器手动更改模式为Mode.ServerDone,我的代码就可以完美地工作。

我最好的猜测是,发生这种情况是因为回调是由一个没有能力写属性的协程调用的,但我不知道如何解决它

Unity3d:脚本改变属性值,但什么也没发生

我找到了答案,原来StartServer()不是异步的,因此我的模式被替换了