Tanmy Modi
Tanmy Modi

Reputation: 23

Showing error in CS1002 and CS0116 in C# script in Unity Engine

enter image description hereI have followed the tutorial on how to make a game of Brackeys from YouTube and I'm stuck on video no. 7 and I can't figure out how to resolve the problem. Please help.

link to tutorial

The code is at 7:36

using UnityEngine;
using UnityEngine UI;

public class Score : MonoBehaviour
{
       public Transform player;
       public Text scoreText;

       void Update()
       {
            scoreText.text = player.position.z.ToString("0");
        }
}

Upvotes: 2

Views: 1022

Answers (2)

Arcanum Astronaut
Arcanum Astronaut

Reputation: 31

The error is in the second line of your code:

using UnityEngine;
using UnityEngine UI;

Should be changed to:

using UnityEngine;
using UnityEngine.UI;

You need a period to access another Namespace within the UnityEngine Namespace.

Upvotes: 3

Jee
Jee

Reputation: 977

First, check the second line of your code. using UnityEngine UI; appears to missing a period between UnityEngine and UI. I believe this is the core of your problem and is causing the other two errors to pop up. The line should be using UnityEngine.UI;.

Regardless, here is an explanation on how to figure out the route of a problem like this.

Please see the following two links for information regarding your two error codes. Remember that these are always available via a quick Google search:

  • CS1002: this error signifies that you are missing a semicolon (;). I C#, a semicolon is required at the end of almost all lines. Looking at your code, the missing semicolon is not within this specific code block, so it must be elsewhere in your code. To find it, the easiest method is ton go to the Unity Console Window and double click the error line or look for the script location and line number in the message. Either of these should bring you close to where the missing semicolon is.
  • CS0116: This error signifies the a namespace that contains something other than a class, struct, or other namespace. This means that the structure of your code is flawed. Again go to to the error and see if you can find the script that this is contained in. Then review the structure and ensure that there aren't any areas where a something like a method falls outside of your class (this is common and would mean that you placed a method directly into a namespace, which will result in CS0116.

Hope this helps!

Edit: thinking a minute more on the logic here and it's 100% the second line. Imagine how the compiler would interpret this. What it really see's when you miss that period is "two lines"

using UnityEngine
UI;

So it's looking at this and saying two things:

  1. You're missing a semicolon after using UnityEngine
  2. You're to do something in the namespace area before your class that is not allowed

Kinda interesting when you think about why it is telling you that you have both of those errors.

Upvotes: 3

Related Questions