C# (42,18):错误CS1525:意外符号(“,应为“,;”,或=

C# (42,18):错误CS1525:意外符号(“,应为“,;”,或=,c#,unity5,C#,Unity5,我在unity中制作一个游戏,在那里我将制作一个时间系统。但是我得到了这个错误42,18:错误CS1525:意外符号',期望',;'或='并且我不知道为什么我不想要工作 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TimeManager : MonoBehaviour { public int seco

我在unity中制作一个游戏,在那里我将制作一个时间系统。但是我得到了这个错误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:error CS1525:Unexpected symbol',预期为',;'或'


谢谢您的帮助。

您已将时间函数嵌套在timeAdd中,我假设您不支持C 7本地函数。请将时间函数从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);
    }
}

将}放在IEnumerator time{code之前,并删除代码末尾的}。另请参见。您的time函数嵌套在timeAdd函数中。这可能是有效的C7代码,但我不相信unity编辑器支持C7。您可以通过移动整个IEnumerable time{/*code*/}来修复它时间添加功能之外。为了更好地回答您未来的问题,请阅读关于发布代码的指南。