Does anyone know how to fix this error?

So I’ve been following a tutorial but when I let the code compile it came back with this error message:

Assets\Scripts\MainMenu.cs(13,52): error CS1519: Invalid token ‘;’ in class, struct, or interface member declaration

Now here is the code I’ve got and the line of code supposedly in question. It seems to compile fine for the tutorial guide but not for me!


All help is appreciated!

A couple of things:

  1. When posting code, post the actual code - not a screenshot. Make sure that you tag it appropriately. Check the FAQ for how to do that.

  2. When following tutorials, make sure that you follow them exactly. Your error is that you have not specified a type for energyRechargeDuration.

You’re welcome!

DateTime is the property in System class. You need to write using System; at the top of your script to access DateTime property

This is the code I have written. The tutor said to specify it as a float for energyRechargeDuration. I have changed that down below. However, a new error comes up after which is this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour
{

[SerializeField] private TMP_Text highScoreText;
[SerializeField] private TMP_Text energyText;
[SerializeField] private int maxEnergy;
[SerializeField] private float energyRechargeDuration;

private int energy;

private const string EnergyKey = "Energy";
private const string EnergyReadyKey = "EnergyReady";

private void Start()
{
    int highScore = PlayerPrefs.GetInt(ScoreSystem.HighScoreKey, 0);

    highScoreText.text = $"High Score: {highScore}";

    energy = PlayerPrefs.GetInt(EnergyKey, maxEnergy);

    if(energy == 0)
    {
        string energyReadyString = PlayerPrefs.GetString(EnergyReadyKey, string.Empty);

        if(energyReadyString == string.Empty) { return; };

        DateTime energyReady = DateTime.Parse(energyReadyString);

        if(DateTime.Now > energyReady)
        {
            energy = maxEnergy;
            PlayerPrefs.SetInt(EnergyKey, energy);
        }
  
    }

    energyText.text = $"Play ({energy})";
}

public void Play()
{
    SceneManager.LoadScene(1);
}

}

Getting to understand error messages is a really important skill to develop early on. Let me break down the error you got:

[13:30:06] – This is just the timestamp when the error occurred.

Assets\Scripts\MainMenu.cs(34,13): – the filename, line number and position in line where the error occurred. This is useful is you compile several files at once. In Unity, you can double click on this error message and it will open the file for you at the right line in your editor.

error cs0246 – the unique error ID. You can find explanations here. Having said that, they can often be quite abstruse with highly specific language such as “directive” and “assembly reference”.

The type or namespace ‘DateTime’ could not be found – This is most important part. A Type is what is often called a data type (float, GameObject or in this case DateTime). A Namespace is a library containing code which might define new types. For example, if you code Unity Events, you need to add a new namespace called UnityEngine.Events, which is where the Event System definitions live. You add new Namespaces to your program by adding the “using” directive at the top of your file. You already have some of these.

(are you missing a directive or an assembly reference?) – This is the C# compiler trying to be helpful. The “using” keyword is what’s called a “directive”. It tells the compiler to add additional libraries (Namespaces) to the compilation process. You can think of an Assembly Reference as a pointer to some external file (for example a DLL or an API) where a type definition can be found but the Assembly Reference doesn’t contain the type definition itself, unlike a Namespace.

So, in this case, the compiler is saying that it doesn’t understand DateTime and suggests that you have a Namespace missing. If you search the web for “c# date time namespace”, you’ll find a link to the Microsoft document where you can find the official definition containing “Namespace: System”. So that tells you the namespace that you needed to add. As @mustafanoorani04 correctly says, you need to add using System; at the top of your file.

It’s good to become an Error Message Ninja - you’ll save loads of time :o)