【问题标题】:Unity: error CS1519: Invalid token '=' in class, struct, or interface member declaration,Unity:错误 CS1519:类、结构或接口成员声明中的标记“=”无效,
【发布时间】:2020-03-05 14:56:53
【问题描述】:

我是编码新手,根据我收集到的信息,编写以下代码是为了与 C# 4.0 而不是当前版本兼容。还有一个我无法在标题中出现的错误: 令牌无效 ';'在类、结构或接口成员声明中

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

public class camera_controller : MonoBehaviour
{

    public GameObject player;
    private Vector3 offset;
    Vector3 offset = transform.position;


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

    }

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

    }

    void LateUpdate () 
    {

    transform.position=player.transform.position+offset;

    }

}

【问题讨论】:

  • 我是编码新手 那么你应该从学习一些 C# 基础知识开始......我敢打赌你把这段代码直接放在类体而不是方法体中......但是没有MVCE只是瞎猜

标签: c# unity3d syntax-error


【解决方案1】:

您应该删除Vector3 offset = transform.position; 并在Start()Awake() 下添加offset = transform.position;,并查看以下链接了解更多信息。除了常量、构造函数和静态字段之外,C# 不允许使用字段初始值设定项。 transform.position 是一个非静态字段。

Why can't I assign transform.position to a Vector3 object?

【讨论】:

    【解决方案2】:

    你需要为变量定义一个类型。

    transform.position 是 Vector3 类型。

    试试这个:

    Vector3 偏移 = transform.position;

    【讨论】:

    • 错误 CS0102:“camera_controller”类型已包含“偏移”的定义
    • 两件事:你不能在同一个范围内有两个同名的变量(偏移量和偏移量),你需要把你在函数中设置值的那一行放在里面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多