Unity 3D 控制台显示:语法错误,'(' 预期
Unity 3D Console says: Syntax error, '(' expected
我是 c# 的新手,对于我的任何困惑,我深表歉意。
我已经使用 Unity 大约一个星期了,我尝试了很多我知道的东西,但没有任何效果。提前致谢!!
using UnityEngine;
namespace UnityStandardAssets.Utility
{
[RequireComponent(typeof (UI.text))]
public class FPSCounter : MonoBehaviour
{
const float fpsMeasurePeriod = 0.5f;
private int m_FpsAccumulator = 0;
private float m_FpsNextPeriod = 0;
private int m_CurrentFps;
const string display = "{0} FPS";
private UI.text m_UI.text;
private void Start()
{
m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
m_UI.text = GetComponent<UI.text>();
}
private void Update()
{
// measure average frames per second
m_FpsAccumulator++;
if (Time.realtimeSinceStartup > m_FpsNextPeriod)
{
m_CurrentFps = (int) (m_FpsAccumulator/fpsMeasurePeriod);
m_FpsAccumulator = 0;
m_FpsNextPeriod += fpsMeasurePeriod;
m_UI.text.text = string.Format(display, m_CurrentFps);
}
}
}
}
Errors From Console
的注释是对的,不能在变量名中使用.
。但是你可以使用 _
.
这里是Microsoft变量命名的基本规则:
The first character of a variable name must be either a letter, an underscore character (_), or the at symbol (@).
Subsequent characters may be letters, underscore characters, or numbers.
There are also certain keywords that have a specialized meaning to the C# compiler
还有一件事可能会在您的代码中产生错误:
从第 [RequireComponent(typeof (UI.text))]
行开始,您使用 UI.text
和小写的 text
。 如果不是这里用的UnityEngine.UI
也可以
但如果它是您尝试使用的,正确的语法是 UI.Text
,大写 T
.
字母大小写在类型和变量名称中很重要。
此外,如果您想避免每次都使用 UI.Text
,您还可以在文件顶部的第一个 using 语句下添加行 using UnityEngine.Text;
。然后,您可以将 [RequireComponent(typeof (UI.text))]
重写为 [RequireComponent(typeof (Text))]
.
编辑:似乎 在我撰写答案时评论了 UI.Text
部分答案。
我是 c# 的新手,对于我的任何困惑,我深表歉意。 我已经使用 Unity 大约一个星期了,我尝试了很多我知道的东西,但没有任何效果。提前致谢!!
using UnityEngine;
namespace UnityStandardAssets.Utility
{
[RequireComponent(typeof (UI.text))]
public class FPSCounter : MonoBehaviour
{
const float fpsMeasurePeriod = 0.5f;
private int m_FpsAccumulator = 0;
private float m_FpsNextPeriod = 0;
private int m_CurrentFps;
const string display = "{0} FPS";
private UI.text m_UI.text;
private void Start()
{
m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
m_UI.text = GetComponent<UI.text>();
}
private void Update()
{
// measure average frames per second
m_FpsAccumulator++;
if (Time.realtimeSinceStartup > m_FpsNextPeriod)
{
m_CurrentFps = (int) (m_FpsAccumulator/fpsMeasurePeriod);
m_FpsAccumulator = 0;
m_FpsNextPeriod += fpsMeasurePeriod;
m_UI.text.text = string.Format(display, m_CurrentFps);
}
}
}
}
Errors From Console
.
。但是你可以使用 _
.
这里是Microsoft变量命名的基本规则:
The first character of a variable name must be either a letter, an underscore character (_), or the at symbol (@).
Subsequent characters may be letters, underscore characters, or numbers.
There are also certain keywords that have a specialized meaning to the C# compiler
还有一件事可能会在您的代码中产生错误:
从第 [RequireComponent(typeof (UI.text))]
行开始,您使用 UI.text
和小写的 text
。 如果不是这里用的UnityEngine.UI
也可以
但如果它是您尝试使用的,正确的语法是 UI.Text
,大写 T
.
字母大小写在类型和变量名称中很重要。
此外,如果您想避免每次都使用 UI.Text
,您还可以在文件顶部的第一个 using 语句下添加行 using UnityEngine.Text;
。然后,您可以将 [RequireComponent(typeof (UI.text))]
重写为 [RequireComponent(typeof (Text))]
.
编辑:似乎 UI.Text
部分答案。