簡體   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

我正在嘗試學習使用 Unity 創建 2D 視頻游戲,但由於幾個錯誤,我無法為 CharacterMovement 編譯我的腳本。 即使創建一個新的空腳本,編譯器也會說“未定義或導入預定義類型 System.Void”,我無法在網上找到解決此問題的方法。

這是空腳本:

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()
    {

    }
}

這是我要編譯的腳本:

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;
    }
}

(在我的腳本上,我得到了大約 60 個類似的錯誤)

File->BuildSettings->playersettings-api 兼容級別中的選項更改為 .Net 4X

對於將來遇到此問題的人來說,另一種可能的解決方案是:轉到編輯->首選項->外部工具->重新生成項目文件以恢復原始 CS 項目和 VS 解決方案文件。

這是在嘗試其他常見答案(如更改 API 兼容性、升級 VS 或刪除項目 obj 文件夾)之后唯一有效的選項。 我認為它是由於我試圖手動清理解決方案而導致的,而 Unity 在我試圖刪除它們后最終損壞了一些包。 所以不幸的是,如果您嘗試這樣做,您似乎會收到此錯誤。

當我嘗試使用Install-Package Microsoft.Toolkit.Uwp.Notifications -Version 7.1.2安裝Microsoft.Toolkit.Uwp.Notification時發生這種情況。

一旦我這樣做了,我注意到一個新的配置文件被添加到我的解決方案資源管理器中。 我必須刪除它以修復錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM