C# 意外符号'=';在类、结构或接口声明中

C# 意外符号'=';在类、结构或接口声明中,c#,debugging,unity3d,C#,Debugging,Unity3d,请原谅并纠正我代码中的错误,我有点傻 我最近在随意编写一个脚本,用铰链电机控制一辆汽车,我想,为什么不使用unity文档呢。我去把代码转移到我的项目中: using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Start() { HingeJoint hinge = GetComponent<HingeJoint>(); JointMo

请原谅并纠正我代码中的错误,我有点傻

我最近在随意编写一个脚本,用铰链电机控制一辆汽车,我想,为什么不使用unity文档呢。我去把代码转移到我的项目中:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
void Start() {
    HingeJoint hinge = GetComponent<HingeJoint>();
    JointMotor motor = hinge.motor;
    motor.force = 100;
    motor.targetVelocity = 90;
    motor.freeSpin = false;
    hinge.motor = motor;
    hinge.useMotor = true;
    }
}
使用UnityEngine;
使用系统集合;
公共类示例类:单一行为{
void Start(){
铰链关节铰链=GetComponent();
接头电机=铰链电机;
电机力=100;
motor.targetVelocity=90;
motor.freeSpin=false;
铰链电机=电机;
铰链.useMotor=true;
}
}
我试图更改代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WHEELSPIN : MonoBehaviour {

    HingeJoint hinge = GetComponent<HingeJoint>();
    private JointMotor motor = hinge.motor;
    hinge.useMotor = true;
    hinge.motor = float motorz;
    motorz.freeSpin = true;


    private void Update()
     {
        if (Input.GetAxis("Vertical"))
        {
            motorz.force = 1000;
            motorz.targetVelocity = 900;
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共级车轮旋转:单一行为{
铰链关节铰链=GetComponent();
专用接头电机=铰链电机;
铰链.useMotor=true;
铰链电机=浮动电机Z;
motorz.freeSpin=true;
私有void更新()
{
if(Input.GetAxis(“垂直”))
{
motorz.force=1000;
motorz.targetVelocity=900;
}
}
}
之前有人建议我不要使用与对象相同的变量,因此它被称为motorz。我还被告知不要在start函数中声明变量,否则我不能在其他函数中使用它,我也改变了这一点,但现在我收到一条错误消息,说“=”符号是意外的。我怎样才能解决这个问题


编辑:忘记提到此脚本将进入汽车的后轮,而不是实际的汽车对象。

我无法测试此脚本,因为我没有您正在使用的库,但这应该会有所帮助

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WHEELSPIN : MonoBehaviour 
{

    HingeJoint hinge;
    JointMotor motorz;

    private void Awake()
    {
        hinge = GetComponent<HingeJoint>();
        motorz = hinge.motor;
        hinge.useMotor = true;
        motorz.freeSpin = true;
    }

    private void Update() 
    {       
        if (Input.GetAxis("Vertical") > 0f) 
        {
            motorz.force = 1000;
            motorz.targetVelocity = 900;
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共级车轮旋转:单一行为
{
铰链;
联合电动机;
私人空间
{
铰链=GetComponent();
motorz=铰链电机;
铰链.useMotor=true;
motorz.freeSpin=true;
}
私有void更新()
{       
if(Input.GetAxis(“垂直”)>0f)
{
motorz.force=1000;
motorz.targetVelocity=900;
}
}
}

我无法测试这一点,因为我没有您正在使用的库,但这应该会有所帮助

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WHEELSPIN : MonoBehaviour 
{

    HingeJoint hinge;
    JointMotor motorz;

    private void Awake()
    {
        hinge = GetComponent<HingeJoint>();
        motorz = hinge.motor;
        hinge.useMotor = true;
        motorz.freeSpin = true;
    }

    private void Update() 
    {       
        if (Input.GetAxis("Vertical") > 0f) 
        {
            motorz.force = 1000;
            motorz.targetVelocity = 900;
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共级车轮旋转:单一行为
{
铰链;
联合电动机;
私人空间
{
铰链=GetComponent();
motorz=铰链电机;
铰链.useMotor=true;
motorz.freeSpin=true;
}
私有void更新()
{       
if(Input.GetAxis(“垂直”)>0f)
{
motorz.force=1000;
motorz.targetVelocity=900;
}
}
}

在WheelSpin类中,您正在类的构造函数中指定铰链。但是,单行为是由Unity引擎任意构造的(当Unity序列化类实例时,可能会因为很多原因发生这种情况)。相反,应该使用Start()和Awake()回调来初始化变量。

在WheelSpin类中,您正在类的构造函数中分配铰链。但是,单行为是由Unity引擎任意构造的(当Unity序列化类实例时,可能会因为很多原因发生这种情况)。相反,应该使用Start()和Awake()回调来初始化变量。

我想您可能被字段初始值设定项弄糊涂了。但在解释什么是字段初始值设定项之前,我应该先从如何正确定义类开始

c#中的类就像现实世界中的契约或模子。定义类后,可以使用它创建实例(就像使用模具在真实世界中铸造对象一样)。同一类的每个实例都遵循该类中定义的行为。它有一定的字段来存储数据和操作方法

下面是类通常的定义方式:

// Double slashes starts a one-line comment
// Comments will be **ignored** when the program is running
// So comments are actually memos for programmers
// Here the class is named as "Creature"
class Creature
{
    // Here you can put some fields that will be used to store data
    int _heathPoint;

    // _healthPoint = 10; // Not permitted because this is not a field declaration

    // Here you can define methods that can be operated with
    public void DoSomething()
    {
        // A method can be empty, just do nothing.
    }
}
您可能会看到,在这个类中,我定义了一个字段
\u healthPoint
和一个方法
DoSomething
,没有其他定义。事实上,在类的“范围”(在
关键字之后由第一级曲线括号括起的区域)中,只允许字段和方法声明(当然,不完全允许,实际上可以添加嵌套类。但这超出了本问答的范围)

如果您创建类
生物
的新实例,此实例将有一个整数字段
\u heatpoint
和一个方法
DoSomething
。但是它的领域的价值是什么呢?在上面的定义中,它没有任何关于初始值如何的单词,因此
\u healthPoint
将被设置为其默认值:对于
int
数据,它意味着
0

在大多数情况下,这样的字段值默认值不是我们想要的。在我们的例子中,将初始
\u healthPoint
设置为
0
生物
实例没有任何意义。假设我们希望它的初始值
\u healthPoint
100
。我们可以这样做:

class Creature
{
    int _heathPoint;

    // Below is something new.
    // Please note that although it looks like an ordinary method like DoSomething(),
    // it lacks the return type and has the same name as the class name
    // Such a method is called **constructor**
    public Creature()
    {
        _healthPoint = 100;
    }

    public void DoSomething()
    {
    }
}
在这里,您可以看到我们添加了一个新方法,该方法与其宿主类同名。这种方法称为构造函数。顾名思义,当您试图构造一个类的实例时,会自动调用构造函数。
然而,这并不意味着构造函数创建实例,它们实际上用于初始化
实例。调用构造函数时,实例已经创建。但是细节超出了这里的范围。
你需要知道的是,这里
生物的构造函数将
\u healthPoint
的值初始化为
1
class ExampleClass : MonoBehaviour
{
    HingeJoint hinge = InitializeHinge();

    private HingeJoint InitializeHinge()
    {
        // Note that the line below compiles but may not run or will report runtime error
        // I am not sure if you can call GetComponent() in field initializer
        HingeJoint returnValue = GetComponent<HingeJoint>();
        returnValue.useMotor = true;
        return returnValue;
    }
}