简体   繁体   English

错误消息显示“类型或命名空间定义,或预期文件结尾”

[英]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.

相关问题 c#错误消息-类型或名称空间定义或预期的文件结尾 - c# Error Message - Type Or namespace definition or end of file expected 错误信息。 类型或命名空间定义,或预期的文件结尾 - ERROR message. Type or namespace definition, or end-of-file expected Unity 控制台显示“类型或命名空间定义,或文件结尾预期统一” - Unity Console says "type or namespace definition, or end of file expected unity" “类型或命名空间定义,预期文件结尾”错误 - “Type or Namespace definition, End of File expected” Error Unity 显示“错误 CS1022:类型或命名空间定义,或预期的文件结尾” - Unity says "error CS1022: Type or namespace definition, or end-of-file expected" 我遇到消息错误 CS1022: Type or namespace definition, or end-of-file expected - I'm having a problem with the message error CS1022: Type or namespace definition, or end-of-file expected 类型或名称空间定义,或文件末尾预期错误 - Type or namespace definition, or end-of-file expected error Unity 给出“类型或命名空间定义,或预期文件结尾”错误 - Unity gives “Type or namespace definition, or end-of-file expected” error 获取“名称空间定义的类型,或文件末尾预期错误” - Getting “ type of namespace definition, or end-of-file expected error” 错误:类型或名称空间定义,或预期的文件结尾 - Error: Type or namespace definition, or end-of-file expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM