[英]Error Message says "Type or namespace definition, or end of file expected"
I'm not sure what's up with my code and please bare in mind I have just started coding.我不确定我的代码是怎么回事,请记住我刚刚开始编码。 please help!请帮忙!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public RigidBody rb
{}
public float forwardForce = 2000f;
public float sidewaysForce = 2000f;
void Update()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("w"))
{
rb.AddForce(500 * Time.deltaTime, 0, 0);
}
}
}
For the error you mention to go away you need to remove the last }
in your code.要消除您提到的错误,您需要删除代码中的最后一个}
。 You will also need to declare a namespace
and a class
as you cannot declare fields and methods without those.您还需要声明一个namespace
和一个class
,因为没有它们就无法声明字段和方法。 You should also initialize your RigidBody
.您还应该初始化RigidBody
。 In the end your code should look something like this:最后,您的代码应如下所示:
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace SomeNamespace
{
public class SomeClass
{
public RigidBody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 2000f;
void Start()
{
/// Initialize rb here
}
void Update()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("w"))
{
rb.AddForce(500 * Time.deltaTime, 0, 0);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.