简体   繁体   English

如何修复“必须先修复所有编译器错误才能进入播放模式!” Unity中的错误

[英]How do I fix "All compiler errors have to be fixed before you can enter playmode!" error in Unity

I'm a new programmer, and I've been trying to make a simple video game in unity, however I can't test my progress so far because I cannot enter the playmode in Unity... I get the message "All compiler errors have to be fixed before you can enter playmode," and I go to check the errors: and they are the 5 following:我是一名新程序员,我一直在尝试统一制作一个简单的视频游戏,但是到目前为止我无法测试我的进度,因为我无法在 Unity 中进入游戏模式......我收到消息“所有编译器必须先修复错误才能进入播放模式”,然后我 go 来检查错误:它们是以下 5 个:

  • error CS1519: invalid token '{' in class, record, struct or interface member declaration错误 CS1519:class、记录、结构或接口成员声明中的令牌“{”无效

  • error CS1519: invalid token '(' in class, record, struct or interface member declaration错误 CS1519:class、记录、结构或接口成员声明中的令牌“(”无效

  • error CS8124: Tuple must contain atleast two elements.错误 CS8124:元组必须包含至少两个元素。

  • error CS1519: invalid token ';'错误 CS1519:无效令牌“;” in class, record, struct or interface member declaration在 class 中,记录、结构或接口成员声明

  • error CS1022: Type or namespace definition, or end of file expected错误 CS1022:类型或命名空间定义,或预期文件结尾

And this is the code:这是代码:

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

public class InputManager : MonoBehaviour
{
    private PlayerInput playerInput;
    private PlayerInput.OnFootActions onFoot;

    private PlayerMotor motor;
    // Start is called before the first frame update
    void Awake()
    {
        playerInput = new PlayerInput();
        onFoot = playerInput.OnFoot;
        motor = GetComponent<PlayerMotor>();
        onFoot.Jump.performed += ctx => motor.Jump();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        //tell the playermotor to move using the value from our movement action.
        motor.ProcessMove(onFoot.Movement.ReadValue<Vector2>());
    }

    private void OnEnable()
    {
        onFoot.Enable();
    }

    private void OnDisable();
    {
        onFoot.Disable();
    }

}

Could anyone tell what the mistakes mean and how I could fix them?谁能告诉我这些错误的含义以及我该如何解决它们?

Thank you in advance: :)先感谢您: :)

If you ever stumble upon error messages and don't understand their description, try to look up their codes in the internet.如果您偶然发现错误消息并且不理解它们的描述,请尝试在互联网上查找它们的代码。 For example https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1519 can give you some insights about your first error.例如https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1519可以为您提供有关第一个错误的一些见解。 The same goes for other stuff.其他东西也是如此。

Regarding error CS1519: invalid token '{' in class, record, struct or interface member declaration you can read in the documentation:关于错误 CS1519: invalid token '{' in class, record, struct or interface member declaration您可以在文档中阅读:

This error is generated whenever a token is encountered in a location where it does not belong.每当在不属于它的位置遇到令牌时,都会生成此错误。 A token is a keyword;令牌是关键字; an identifier (the name of a class, struct, method, and so on);标识符(class、结构、方法等的名称); a string, character, or numeric literal value such as 108, "Hello", or 'A';字符串、字符或数字文字值,例如 108、“Hello”或“A”; or an operator or punctuator such as == or;.或运算符或标点符号,例如 == 或;。

In your case the token is ;在您的情况下,令牌是; and it doesn't belong in here: private void OnDisable();它不属于这里: private void OnDisable();

This semicolon is the issue, as it made your method bodyless (when it should have one), as well as your method call onFoot.Disable();这个分号是问题所在,因为它使您的方法没有主体(当它应该有一个时),以及您的方法调用onFoot.Disable(); is now treated as if made just inside a class, not method, which is not valid.现在被视为仅在 class 内部制作,而不是方法,这是无效的。

In general, this error usually indicate typos of all kind.通常,此错误通常表示各种类型的拼写错误。

error CS1022: Type or namespace definition, or end of file expected is just a consequence of formerly mentioned semicolon, that propagates further down the file.错误 CS1022:类型或命名空间定义,或预期的文件结尾只是前面提到的分号的结果,它会在文件中进一步传播。

As for error CS8124: Tuple must contain atleast two elements.至于错误 CS8124:元组必须包含至少两个元素。 I can't see this error in the code I pasted to my project, maybe that's an error in another file you didn't paste here.我在粘贴到我的项目的代码中看不到这个错误,也许这是你没有粘贴到这里的另一个文件中的错误。 But the message clearly indicates the reason: you are using a tuple, that contain less than 2 elements.但是该消息清楚地表明了原因:您正在使用一个包含少于 2 个元素的元组。 Alternatively, it might be another typo, that made compiler think you are using a tuple.或者,它可能是另一个错字,使编译器认为您正在使用元组。

Suggestions:建议:

(Correct me if I am wrong) you have copied the code from somewhere and trying to make it work in your game right? (如果我错了,请纠正我)你从某个地方复制了代码并试图让它在你的游戏中工作,对吗?

So for the namespace error it might be because you haven't imported new inputSystem package from package manager in unity and as far as I understood your code other errors don't belong to this script so perhaps you have some other scripts in your project that are causing those errors when compiling...因此,对于命名空间错误,这可能是因为您没有从 package 管理器统一导入新的 inputSystem package,据我了解,您的代码其他错误不属于此脚本,因此您的项目中可能还有其他一些脚本编译时导致这些错误...

so basically that error cannot be resolved (or in other words you can't get into play mode) unless your scripts compile correctly (without any issues/errors) so there is way with which you can check in which script is throughing that errors and on which line so you just need to go to your console of unity editor ( shortcut for windows: Ctrl + Shift + C ) and double click on error to see which line is causing the problem.因此,除非您的脚本正确编译(没有任何问题/错误),否则基本上无法解决该错误(或者换句话说,您无法进入播放模式),因此您可以通过某种方式检查哪个脚本正在通过该错误和在哪一行,所以你只需要 go 到你的统一编辑器控制台(windows 的快捷方式:Ctrl + Shift + C 导致问题)并双击错误

your errors meaning:你的错误意味着:

1st error means that you have an extra or less '{' curly bracket in your script.第一个错误意味着您的脚本中有一个额外或更少的“{”花括号。

2nd error means that you have an extra or less '(' parentheses (also called small bracket) in your script.第二个错误意味着您的脚本中有一个额外或更少的 '(' 括号(也称为小括号)。

so maybe by mistake you have inserted those brackets in your code or maybe you have removed one side of the bracket in your code that also can be the reason why those errors occuring.因此,您可能错误地在代码中插入了这些括号,或者您在代码中删除了括号的一侧,这也可能是发生这些错误的原因。 (solution: check your scripts and put or delete brackets which are missing or if there is any extra) (解决方案:检查您的脚本并放置或删除缺少的括号或是否有额外的括号)

3rd error means that you have a 'tuple' data type variable but it doesn't contain least number of elements in it.第三个错误意味着您有一个“元组”数据类型变量,但其中不包含最少数量的元素。

so basically tuple is a data type which stores multiple type of variables in one variable (made it simple for you to understand), so as I told you before check and double click on that error and it will lead you to the line which is causing error and try to fix it then.所以基本上元组是一种数据类型,它在一个变量中存储多种类型的变量(让你更容易理解),所以正如我在检查并双击该错误之前告诉你的那样,它会引导你到导致的行错误并尝试修复它。

4rth error means (again the same like the bracket ones) that you an extra or less ';'第 4 个错误意味着(再次与括号相同)您额外或更少的 ';' semi colon in your script.脚本中的分号。

5th error means you are missing a reference of a namespace which in your case (if I am not wrong) is because you haven't imported the library / package of input system so...第 5 个错误意味着您缺少对命名空间的引用,在您的情况下(如果我没记错的话)是因为您没有导入输入系统的库 / package 所以......

Anyways hope you understood something new from it and I am sorry if I made it so confusing...无论如何,希望你能从中理解一些新的东西,如果我把它弄得如此混乱,我很抱歉......

hope it helps... Happy coding:)希望它有所帮助......快乐的编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM