-4

I am making a game in unity, where i will make a time system. But im getting this error "(42,18): error CS1525: Unexpected symbol (', expecting,', ;', or='" and i can not find out why i doesnt want work.

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);
            }
        }
    }
}

What would work better/at all? Right now im getting the error "(42,18): error CS1525: Unexpected symbol (', expecting,', ;', or='"

Thanks for your help.

Jonathon Chase
  • 9,396
  • 21
  • 39
  • Put a `}` before the `IEnumerator time() {` code and remove the `}` at the end of your code. Also see https://stackoverflow.com/questions/5884319/c-function-in-function-possible . – mjwills Dec 08 '17 at 22:59
  • Your time() function is nested in the timeAdd function. This would be valid C# 7 code, but I don't believe that the unity editor has C# 7 support. You can fix it by moving the whole `IEnumerable time() { /* code */ }` outside of the timeAdd function. – Jonathon Chase Dec 08 '17 at 23:05
  • To make your future questions better please read [MCVE] guide on posting code. – Alexei Levenkov Dec 08 '17 at 23:25

1 Answers1

1

You've nested the time() function inside of timeAdd(), and I'm assuming you don't have C# 7 support for local functions. Pull the time() function out of timeAdd() to look like this:

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);
    }
}
Jonathon Chase
  • 9,396
  • 21
  • 39