简体   繁体   English

类型或命名空间定义,或预期的文件结尾 (22,1) (UNITY)

[英]Type or namespace definition, or end-of-file expected (22,1) (UNITY)

Please fix my script, something is wrong!请修复我的脚本,有问题! I get this error, and I don't know what's wrong!我得到这个错误,我不知道出了什么问题! There's something wrong at the 22nd line.第 22 行有问题。

using System;
using System.Diagnostics;
using System.Web;
using UnityEngine;

public class Target : MonoBehaviour
{
    // Start is called before the first frame update

    public float health = 50f;


    public void TakeDamage (float amount)
    {
        health -= amount;
        if (health <= 0f)
        {
            Die();
        }
    }

    void Die()
    {
        Destroy(gameObject);
    }

}

Easiest things to check:最容易检查的事情:

  • This error comes up a lot when you've accidentally put the wrong number of closing curly braces.当您不小心放置了错误数量的右花括号时,会经常出现此错误。 However I didn't see any problems in the code you posted.但是,我在您发布的代码中没有看到任何问题。
  • Double check that there are no extra curly braces or text at the very end of your file.仔细检查文件末尾是否没有多余的花括号或文本。

It's possible that even though you wrote using UnityEngine;即使您using UnityEngine; , your code editor is bugging out and failing to recognize it. ,您的代码编辑器出现故障并且无法识别它。 VSCode has been particularly finicky for me lately in similar ways. VSCode 最近以类似的方式对我来说特别挑剔。 Here are some things you can try:以下是您可以尝试的一些事情:

  • Instead of Destroy(gameObject);而不是Destroy(gameObject); try putting UnityEngine.GameObject.Destroy(gameObject);尝试把UnityEngine.GameObject.Destroy(gameObject); This will force the compiler to use the right namespace for that function.这将强制编译器为该函数使用正确的命名空间。
  • If that doesn't help, you may need to reset some things: Try creating your Target file again and copying the code over to the new file.如果这没有帮助,您可能需要重置一些东西:尝试再次创建您的目标文件并将代码复制到新文件中。 EDIT: Rocketman Dan pointed out that you may need to manually retype some of the code, because there may be problems with invisible characters.编辑: Rocketman Dan 指出您可能需要手动重新输入一些代码,因为可能存在不可见字符的问题。
  • You may also need to close your project in your code editor and re-open it.您可能还需要在代码编辑器中关闭项目并重新打开它。 You may also need to restart Unity.您可能还需要重新启动 Unity。
  • If you use Visual Studio Code, make sure IntelliSense is working correctly.如果您使用 Visual Studio Code,请确保 IntelliSense 正常工作。

暂无
暂无

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

相关问题 C# 错误:cs(22,1):错误 CS1022:类型或命名空间定义,或预期文件结尾 - C# Error : cs(22,1): error CS1022: Type or namespace definition, or end-of-file expected Unity 给出“类型或命名空间定义,或预期文件结尾”错误 - Unity gives “Type or namespace definition, or end-of-file expected” error 类型或名称空间定义,或预期的文件结尾 - Type or namespace definition, or end-of-file expected 类型或名称空间定义,或预期的文件结尾。 预期 - type or namespace definition, or end-of-file expected. } expected Unity C# 错误:“类型或命名空间定义,或预期文件结尾” - Unity C# Error: “Type or namespace definition, or end-of-file expected” 错误 CS1022:类型或命名空间定义,或文件结尾预期的统一 3d 游戏引擎 - error CS1022: Type or namespace definition, or end-of-file expected unity 3d game engine Unity 显示“错误 CS1022:类型或命名空间定义,或预期的文件结尾” - Unity says "error CS1022: Type or namespace definition, or end-of-file expected" 类型或命名空间定义,或预期在不同文件夹中的文件结尾 - Type or namespace definition, or end-of-file expected on a different folder 类型或名称空间定义,或文件末尾预期错误 - Type or namespace definition, or end-of-file expected error Class 内 class - 类型或命名空间定义,或预期文件结尾 - Class inside class - Type or namespace definition, or end-of-file expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM