简体   繁体   English

尝试在 Visual Studio 2017 中为 Unity 2D 编译 c# 脚本时出现“未定义或导入预定义类型 System.Void”错误

[英]"Predefined type System.Void is not defined or imported" error trying to compile c# scripts for Unity 2D in Visual Studio 2017

I'm trying to learn creating 2D videogames with Unity but i can't compile my script for CharacterMovement because of several errors.我正在尝试学习使用 Unity 创建 2D 视频游戏,但由于几个错误,我无法为 CharacterMovement 编译我的脚本。 Even creating a new empty script, the compiler says that "Predefined type System.Void is not defined or imported" and i couldn't find online a way to fix this.即使创建一个新的空脚本,编译器也会说“未定义或导入预定义类型 System.Void”,我无法在网上找到解决此问题的方法。

This is the empty script :这是空脚本:

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

public class MyPlayerMovement : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}

And this is the script i'm trying to compile :这是我要编译的脚本:

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

public class PlayerMovement : MonoBehaviour {

    public CharacterController2D controller;
    public Animator animator;

    public float runSpeed = 40f;

    float horizontalMove = 0f;
    bool jump = false;
    bool crouch = false;

    // Update is called once per frame
    void Update () {

        horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

        animator.SetFloat("Speed", Mathf.Abs(horizontalMove));

        if(horizontalMove == 0)
        {
            animator.SetBool("Jumping", false);
        }

        if (Input.GetButtonDown("Jump"))
        {
            jump = true;
            animator.SetBool("Jumping", true);
        }

        if (Input.GetButtonDown("Crouch"))
        {
            crouch = true;
            animator.SetBool("Crouching", true);
        } else if (Input.GetButtonUp("Crouch"))
        {
            crouch = false;
            animator.SetBool("Crouching", false);
        }

    }

    void FixedUpdate ()
    {
        // Move our character
        controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
        jump = false;
    }
}

(On my script i get about 60 errors all similar) (在我的脚本上,我得到了大约 60 个类似的错误)

File->BuildSettings->playersettings-api 兼容级别中的选项更改为 .Net 4X

Another possible solution for those stumbling across this in the future: Go to Edit->Preferences->External Tools->Regenerate project files to bring back the original CS Project & VS solution files.对于将来遇到此问题的人来说,另一种可能的解决方案是:转到编辑->首选项->外部工具->重新生成项目文件以恢复原始 CS 项目和 VS 解决方案文件。

That was the only option that worked after trying other common answers like changing API compatibility, upgrading VS, or deleting the project obj folder.这是在尝试其他常见答案(如更改 API 兼容性、升级 VS 或删除项目 obj 文件夹)之后唯一有效的选项。 I think it broke like that due to me trying to clean out the solution manually and Unity ended up corrupting some packages after I attempted to delete them.我认为它是由于我试图手动清理解决方案而导致的,而 Unity 在我试图删除它们后最终损坏了一些包。 So unfortunately it seems you will get this error if you try to do that.所以不幸的是,如果您尝试这样做,您似乎会收到此错误。

This happened when I tried to install Microsoft.Toolkit.Uwp.Notification using Install-Package Microsoft.Toolkit.Uwp.Notifications -Version 7.1.2 .当我尝试使用Install-Package Microsoft.Toolkit.Uwp.Notifications -Version 7.1.2安装Microsoft.Toolkit.Uwp.Notification时发生这种情况。

Once I do this, I notice a new config file is added to my solution explorer.一旦我这样做了,我注意到一个新的配置文件被添加到我的解决方案资源管理器中。 I have to delete this to fix the error.我必须删除它以修复错误。

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

相关问题 “预定义类型'系统。*'未定义或导入”错误,visual studio代码,omnisharp - “Predefined type 'System.*' is not defined or imported” Error, visual studio code, omnisharp C# 中未定义或导入预定义类型“System.String” - Predefined type 'System.String' is not defined or imported in C# C# 未定义或导入预定义类型“System.Object” - C# Predefined type 'System.Object' is not defined or imported Unity 2017 + Visual Studio 2017使用插件Waypoints编译错误 - Unity 2017 + visual studio 2017 compile error with plugin waypointsSystem C#-Visual Studio 2017-获取预定义方法的实际代码 - C# - Visual Studio 2017 - Get actual code of predefined methods 在 Visual Studio Code 上运行 .NET Core Azure 函数时出现问题。 未定义或导入预定义类型“System.Object” - Problem running .NET Core Azure functions on Visual Studio Code. Predefined type 'System.Object' is not defined or imported 未定义或导入预定义类型 System.Range - predefined type System.Range is not defined or imported 未定义或导入预定义类型System.Object - Predefined type System.Object is not defined or imported 未定义或导入预定义类型“System.ValueTuple`2” - Predefined type 'System.ValueTuple`2' is not defined or imported 未定义或导入预定义类型“System.String” - Predefined type 'System.String' is not defined or imported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM