下面的代码有问题吗? 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;
}
}
最佳答案
添加此:
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);
我希望您能够拍摄控制台的屏幕截图,并向我们展示输出!
对于像这样奇怪的控制台问题,需要记住四个关键点
仔细检查控制台调试输出行的来源
单击控制台行,在“底部”查看更多信息
永远不要打印裸露的
Debug.Log(x)
,而是这样做Debug.Log("yo "+x);
不要忘记臭名昭著的“崩溃”问题! 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/