为什么这个脚本会出现解析错误?

时间:2012-11-25 00:07:20

标签: c# unity3d

我此刻并不太了解C#;我只是在光速下通过Unity的速成课程。

我尝试将此脚本从我的桌面拖放到Unity项目视图中的assets文件夹,但它说“解析错误1,13”。我不确定脚本有什么问题。有经验的人可以看一眼吗?

var emitter : ParticleEmitter = GetComponentsInChildren(Partic­­leEmitter);

if (Input.GetButton("Fire1")){ emitter.emit=true;

}

else{

emitter.emit=false;

}

5 个答案:

答案 0 :(得分:3)

我没有自己使用Unity w / C#但是我可以告诉你,你的脚本甚至不是有效的C#语法。 C#变量的输入方式有两种:

  • 隐含地:var x = SomeExpression;其中x自动采用表达式的类型
  • 明确地:ParticleEmitter x = SomeExpression;其中x是ParticleEmitter,且表达式必须属于同一类型。

具体而言,错误是关于第一行中的:。这是非法的C#语法,因为唯一可以合法地出现在该位置的字符是=

答案 1 :(得分:2)

这不是C#,它是用UnityScript编写的(基于JavaScript)。

你可以这样说,因为":"冒号字符在UnityScript中用作类型声明,但在C#中无效。

答案 2 :(得分:1)

从未使用过C#,但是我会在第一行中省略“:ParticleEmitter”部分尝试隐式类型声明...

答案 3 :(得分:0)

GetComponentsInChildren()返回Component的类型。解析错误可能来自编译器尝试协调将Component类型传递给ParticleEmitter类型。

来自Unity Component.GetComponentsInChildren的Unity3D文档的示例可以帮助您:

 // Disable the spring on all HingeJoints 
 // in this game object and all its child game objects
 var hingeJoints : Component[];
 hingeJoints = GetComponentsInChildren (HingeJoint);
 for (var joint : HingeJoint in hingeJoints) {
     joint.useSpring = false;
 }

答案 4 :(得分:0)

正如CC Inc所说,您提供的代码不是C#

我试图为你转换它:

ParticleEmitter emitter = GetComponentsInChildren<ParticleEmitter>().FirstOrDefault();

if (emitter != null)
{
    if (Input.GetButton("Fire1"))
    {
        emitter.emit = true;
    }
    else
    {
        emitter.emit = false;
    }
}

GetComponentsInChildren返回一个数组。因此模仿你已经得到的东西我已经声明我想要第一个,如果你找不到任何给我默认值(这是空的)。

这可能不是您所要求的,您可能只需要以下内容:

bool fire = Input.GetButton("Fire1");
ParticleEmitter[] emitters = GetComponentsInChildren<ParticleEmitter>();
foreach(ParticleEmitter emitter in emitters)
{
    emitter.emit = fire; 
}

上面将存储Input.GetButton的布尔结果,然后循环遍历它找到的所有发射器,并将它们设置为与Input.Getbutton相同的值。

显然上面只是猜测你想要什么,我希望它有所帮助。

相关问题