unity-game-engine - 在 Awake 或 Start 中调用 switch 语句

标签 unity-game-engine

下面的代码有问题吗? Debug 打印正确的 chosenScenario,但无论我测试哪个选项 (1,2,3,4),它都不会出现在 switch block 中... 我是不是做了什么蠢事?

void Awake()
{
    Setup();
}

void Setup()
{
    // Retrieve the user's selected scenario through player preference log.
    string selectedScenario = PlayerPrefs.GetString("selectedScenario");
    int chosenScenario = PlayerPrefs.GetInt("chosenScenario");

    // Print some useful information.
    Debug.Log("Selected Training Scenario is " + selectedScenario + " which is scenario number " + chosenScenario);

    switch (chosenScenario)
    {
        case 1:
                Debug.Log("Made it here 1");
                break;
        case 2:
                Debug.Log("Made it here 2");
                break;
        case 3:
                Debug.Log("Made it here 3");
                break;
        case 4:
                Debug.Log("Made it here 4");
                break;
        default:
                Debug.Log(chosenScenario);
                break;
    }
}

控制台输出的屏幕截图: enter image description here

最佳答案

添加此:

  switch (chosenScenario)
    {
        case 0:
                Debug.Log("Hell, the preference was not saved");
                break;
        case 1:
                Debug.Log("Made it here 1");
                break;
        .. continue as before ..

您将立即看到您的问题。

关于这一点,

Debug.Log("Chosen scenario number " + chosenScenario);

我希望您能够拍摄控制台的屏幕截图,并向我们展示输出!


对于像这样奇怪的控制台问题,需要记住四个关键点

  1. 仔细检查控制台调试输出行的来源

  2. 单击控制台行,在“底部”查看更多信息

  3. 永远不要打印裸露的Debug.Log(x),而是这样做Debug.Log("yo "+x);

  4. 不要忘记臭名昭著的“崩溃”问题! https://stackoverflow.com/a/34713627/294884


您的问题可能是:

I know it is 4 because in a script (run before this one) I set it to 4 so I expect to see the Made it here 4 message, but nothing is printed to console

总是,总是,总是在设置首选项后运行“保存”(这只是 Unity 的愚蠢事情之一)...

PlayerPrefs.SetString(PString, fs);
PlayerPrefs.Save();

这是一个典型的示例例程...

private void WritePrefs()
    {
    List<string> ff = new List<string>();
    foreach (Thingy th in Thingies ) ff.Add(th.Info.handyCode);
    string fs = String.Join(",", ff.ToArray());
    PlayerPrefs.SetString(PString, fs);
    PlayerPrefs.Save();
    }

关于unity-game-engine - 在 Awake 或 Start 中调用 switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35200736/

相关文章:

c# - 通过代码将纹理标记为可读/可写

c# - 我的脚本只更新文本一次

unity-game-engine - Unity 游戏中 TextMeshPro 的本地化

c# - 我无法将游戏对象的位置放入 vector3 变量中

unity-game-engine - 如何确定 Unity 插件是否挂起(来自 unityscript)

c# - 如何统一更改克隆的排序?

c# - 如何在 Unity 中通过 X 坐标将游戏对象居中?

unity-game-engine - 如何确定以前引用但现在丢失的 MonoBehaviour 脚本的名称?

unity-game-engine - 使用 AddressableAssets.LoadAssetsAsync

ios - Xcode iOS 删除框架的权限请求