【问题标题】:Namespace Error in C# scriptC# 脚本中的命名空间错误
【发布时间】:2017-02-25 03:31:15
【问题描述】:

我在 Private Void Awake() 和 Private Void Start() 中出现错误 脚本上的部分。如果有人可以提供帮助,我将不胜感激。

读取的错误:

预期的类、委托、枚举、接口或结构。类型 命名空间定义,或检测到文件结尾。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public GameObject[] m_Tanks;

private float m_gameTime = 0;
public float GameTime { get { return m_gameTime; } }
public enum GameState
{
    Start,
    Playing,
    GameOver
};
private GameState m_GameState;
public GameState State { get { return m_GameState; } }
}

private void Awake()
{
m_GameState = GameState.Start;
}
private void Start()
{
for (int i = 0; i < m_Tanks.Length; i++)
{
    m_Tanks[i].SetActive(false);
}
m_TimerText.gameObject.SetActive(false);
m_Exit.gameObject.SetActive(false);
m_HighScores.LoadScoresFromFile();
}
void Update()
{
switch (m_GameState)
{
case GameState.Start:
    if (Input.GetKeyUp(KeyCode.Return) == true)
    {
        m_GameState = GameState.Playing;
        for (int i = 0; i < m_Tanks.Length; i++)
        {
            m_Tanks[i].SetActive(true);
        }
    }
    break;
case GameState.Playing:
    bool isGameOver = false;
    m_gameTime += Time.deltaTime;
    int seconds = Mathf.RoundToInt(m_gameTime);
    if (OneTankLeft() == true)
    {
        isGameOver = true;
    }
    else if (IsPlayerDead() == true)
    {
        isGameOver = true;
    }
    if (isGameOver == true)
    {
        m_GameState = GameState.GameOver;
    }
    break;
case GameState.GameOver:
    if (Input.GetKeyUp(KeyCode.Return) == true)
    {
        m_gameTime = 0;
        m_GameState = GameState.Playing;
        for (int i = 0; i < m_Tanks.Length; i++)
        {
            m_Tanks[i].SetActive(true);
        }
    }
    break;
}
if (Input.GetKeyUp(KeyCode.Escape))
{
    Application.Quit();
}
}
private bool OneTankLeft()
{
int numTanksLeft = 0;
for (int i = 0; i < m_Tanks.Length; i++)
{
    if (m_Tanks[i].activeSelf == true)
    {
        numTanksLeft++;
    }
}
return numTanksLeft <= 1;
}
private bool IsPlayerDead()
{
for (int i = 0; i < m_Tanks.Length; i++)
{
    if (m_Tanks[i].activeSelf == false)
    {
        if (m_Tanks[i].tag == "Player")
            return true;
    }
}
return false;
}

【问题讨论】:

  • 如果缩进得当,更容易识别大括号不平衡的位置。

标签: c# unity5


【解决方案1】:

您没有正确关闭文件中的花括号 ({})。

您的课程 GameManager 在第 18 行结束,您的方法 Awake 无处开始,这是不正确的。

方法需要放在类中。那是你的问题。

【讨论】:

    猜你喜欢
    • 2012-10-13
    • 2023-03-03
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 2013-06-17
    • 2014-08-01
    • 2011-09-15
    相关资源
    最近更新 更多