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

时间:2019-02-20 15:17:53

标签: c# unity3d 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个类似的错误)

1 个答案:

答案 0 :(得分:1)

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