【问题标题】:(42,18): error CS1525: Unexpected symbol (', expecting,', ;', or= [closed](42,18):错误CS1525:意外符号(',期待,',;',或= [关闭]
【发布时间】:2023-03-03 05:41:22
【问题描述】:

我正在统一制作游戏,我将在其中制作时间系统。但我收到此错误“(42,18):错误CS1525:意外符号(',期待,',;',或='”,我不知道为什么我不想工作。

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

public class TimeManager : MonoBehaviour {

    public int seconds = 0;
    public int minutes = 0;
    public int hours = 0;
    public int days = 0;
    public int year = 0;
    public Text TotalTimePlayed;

    void Start(){
        StartCoroutine(time());
    }

    void Update(){
        TotalTimePlayed = year + " Y" + days + " D" + hours + " H" + minutes + " M" + seconds + " S";
    }

    private void timeAdd(){
        seconds += 1;
        if(seconds >= 60){
            minutes = 1;
        }

        if(minutes >= 60){
            hours = 1;
        }

        if(hours >= 24){
            days = 1;
        }

        if(days >= 365){
            year = 1;
        }

        IEnumerator time() {  // Its in this line there is an error.
            while (true){
                timeAdd();
                yield return new WaitForSeconds(1);
            }
        }
    }
}

什么会更好/根本?现在我收到错误“(42,18):错误CS1525:意外符号(',期待,',;',或='”

感谢您的帮助。

【问题讨论】:

  • IEnumerator time() { 代码之前放置一个} 并删除代码末尾的}。另见stackoverflow.com/questions/5884319/…
  • 您的 time() 函数嵌套在 timeAdd 函数中。这将是有效的 C# 7 代码,但我不相信统一编辑器支持 C# 7。您可以通过将整个 IEnumerable time() { /* code */ } 移到 timeAdd 函数之外来修复它。
  • 为了更好地解决您未来的问题,请阅读minimal reproducible example 发布代码指南。

标签: c# unity5


【解决方案1】:

您已将 time() 函数嵌套在 timeAdd() 中,并且我假设您没有 C# 7 对本地函数的支持。将 time() 函数从 timeAdd() 中拉出,如下所示:

private void timeAdd(){
    seconds += 1;
    if(seconds >= 60){
        minutes = 1;
    }

    if(minutes >= 60){
        hours = 1;
    }

    if(hours >= 24){
        days = 1;
    }

    if(days >= 365){
        year = 1;
    }
}

IEnumerator time() {  // Its in this line there is an error.
    while (true){
        timeAdd();
        yield return new WaitForSeconds(1);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多