c# - “意外符号‘else’”我该如何解决这个问题(if 语句后没有分号)?
问题描述
我在第 56 行遇到错误...有人可以告诉我为什么吗?我没有在 if 语句的末尾放置任何分号,而且我还使用了右括号放置......这里有什么问题?我会很感激一些帮助,谢谢!
这是我的代码:
using UnityEngine;
using System.Collections;
public class PlayerPrefsManager : MonoBehaviour {
const string MASTER_VOLUME_KEY = "master_volume";
const string DIFFICULTY_KEY = "difficulty";
const string LEVEL_KEY = "level_unlocked_";
public static void SetMasterVolume(float volume)
{
if (volume > 0f && volume < 1f) {
PlayerPrefs.SetFloat (MASTER_VOLUME_KEY, volume);
} else {
Debug.LogError ("Master volume out of range");
}
}
public static float GetMasterVolume()
{
return PlayerPrefs.GetFloat (MASTER_VOLUME_KEY);
}
public static void UnlockLevel (int level)
{
if (level <= Application.levelCount - 1) {
PlayerPrefs.SetInt(LEVEL_KEY + level.ToString(), 1); //Use 1 for true
} else {
Debug.LogError("Trying to unlock level that isn't in build settings");
}
}
public static bool IsLevelUnlocked(int level)
{
int levelValue = PlayerPrefs.GetInt (LEVEL_KEY + level.ToString ());
bool isLevelUnlocked = (levelValue == 1);
if (level <= Application.levelCount - 1) {
{
return isLevelUnlocked;
} else {
Debug.LogError("Trying to unlock level that isn't in build settings");
return false;
}
}
}
}
这是我得到的错误:
资产/脚本/PlayerPrefsManager.cs(56,30):错误 CS1525:意外符号“其他”
解决方案
你有一个额外的左括号:
if (level <= Application.levelCount - 1) { //here
{ //also here
在 IsLevelUnlocked()