首页 > 解决方案 > C# Unity 2D 代码中的大括号预期错误

问题描述

我已经尝试了以下 C# 代码来统一,我在} expected [Assembly-CSharp]csharp(CS1513)我标记的大括号上遇到了一个错误,在最后一个大括号上我有一个Type or namespace definition, or end-of-file expected [Assembly-CSharp]csharp(CS1022)错误,当我删除它时它会留下,但是它应该结束 Monobehavior 主体。

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

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

    }
    

    // Update is called once per frame
    void Update()
    { // error here
    public static float ScrollWheel { get { return Input.mouseScrollDelta.y / 10; } }
    
    }

} // and also here

标签: c#unity3dvisual-studio-code

解决方案


具体来说,您在方法中包含了一个属性。但是,请注意,您可以在其他成员中包含本地函数。

如果您的意图是简单地获得该值,您可以像这样构造您的代码:

public class NewBehaviourScript : MonoBehaviour
{
    public static float ScrollWheel => Input.mouseScrollDelta.y / 10;

    // Start is called before the first frame update
    void Start ( )
    {
    }

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

我为 ScrollWheel 值使用了Lambda表达式。


推荐阅读