error CS0246: The type or namespace name could not be found

ConsoleCompiler error · Compiler

error CS0246: The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?)

The compiler met a type name it does not know. Either the namespace is not imported, the package that defines it is not installed, or the name is misspelled.

Read the Console line first

X is the unknown type. The line before the message gives the file and the line and column, such as (12,13).

Common causes

  1. A using directive is missing

    TextMeshProUGUI needs using TMPro; Image and Button need using UnityEngine.UI; NavMeshAgent needs using UnityEngine.AI; List needs using System.Collections.Generic;.

  2. The package is not installed

    Types from packages, such as the Input System or Cinemachine, only exist when the package is in Window > Package Manager.

  3. The name is misspelled or the case is wrong

    C# is case-sensitive: Rigidbody2d is not Rigidbody2D.

  4. An assembly definition does not reference it

    If your scripts live under an .asmdef file, that assembly must list the assembly that defines the type in its references.

  5. The type was renamed in Unity 6

    Cinemachine 3 renamed its namespace to Unity.Cinemachine, and some classes were renamed. Code written for older versions reports CS0246 after an upgrade.

The fix

Fix
C#
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ScoreLabel : MonoBehaviour
{
    [SerializeField] private TextMeshProUGUI label;
}

Stop it coming back

Let your code editor add using directives for you; with Unity's IDE packages installed, it knows every namespace in the project.

Questions

TMP_Text or TextMeshProUGUI cannot be found. What do I add?
Add using TMPro; at the top of the file. In Unity 6, TextMeshPro ships inside the Unity UI package, so make sure Unity UI is installed in the Package Manager. If your scripts use an assembly definition, add a reference to Unity.TextMeshPro.
The InputSystem namespace cannot be found. What is missing?
Install the Input System package from the Package Manager and add using UnityEngine.InputSystem;. With an assembly definition, also add Unity.InputSystem to its references.
My IDE shows no error but Unity does. Who is right?
Unity is; it compiles the code that ships. The IDE's project files can be stale after packages change. Regenerate them from Preferences > External Tools > Regenerate project files, then reopen the solution.

Upgrading to Unity 6?

This error can also come from an API Unity 6 changed. Cinemachine 3 in Unity 6: CinemachineVirtualCamera is now CinemachineCamera.

Scripts that avoid it

Related errors

Got a different error? Paste it into the error lookup.