下面的代码有问题吗?Debug打印正确的chosenScenario,但是无论我测试哪个选项(1,2,3,4),它都不会在switch块中.我是不是在做傻事?
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;
}
}控制台输出的屏幕截图:

发布于 2016-02-04 13:29:56
添加以下内容:
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(“选定的场景号”+ chosenScenario);
我想让您实际拍摄控制台的屏幕截图,并向我们展示输出!
对于这种奇怪的控制台问题,需要记住的四个关键点--
Debug.Log(x),而要做这个Debug.Log("yo "+x);你的问题很可能是:
我知道它是4,因为在脚本中(在这个脚本之前运行),我将它设置为4,所以我希望在这里看到Made it 4消息,但是没有任何东西被打印到控制台。
总是,总是,总是在设置prefs之后运行“保存”(这只是关于统一的愚蠢的事情之一).
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();
}https://stackoverflow.com/questions/35200736
复制相似问题