【问题标题】:Curly Brace Expected Error in C# Unity 2D Code [closed]C# Unity 2D 代码中的大括号预期错误 [关闭]
【发布时间】:2021-03-15 00:11:20
【问题描述】:

我已尝试使用以下 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# unity3d visual-studio-code


【解决方案1】:

在方法内创建属性会导致此问题。如果您的目标是在每一帧中获取鼠标滚动数据(这是另一个优化问题),您可以这样做:

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()
    { 
       //call the method here
       ScrollWheel();
    
    }

     public static float ScrollWheel ();
     {  
        return Input.mouseScrollDelta.y / 10; 
     }
} 

我创建了一个方法而不是上面的属性,但这应该类似。

【讨论】:

    【解决方案2】:

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

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

    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 表达式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-19
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      相关资源
      最近更新 更多