Quick answer: CS0122 means the field, method, constructor or class you named exists, but its access modifier does not let this script use it. In C#, a class member with no modifier is private, so int health; and void TakeDamage() can only be used inside their own class. Add public to the method another script should call, or expose a private field through a public property. If the name in the message is a whole class, it is internal or nested and you are outside its assembly or its containing class.

You wrote player.health or player.TakeDamage(10) from another script, the name is spelled correctly, autocomplete may even have refused to offer it, and the Console says it “is inaccessible due to its protection level”. Unlike most compile errors, nothing is missing. The compiler found the member and is enforcing the visibility its declaration asked for. This guide explains what each access modifier allows, which one applies when you write none, and how to fix the Unity situations that produce CS0122: another script’s fields and methods, [SerializeField] fields, subclasses, singletons and private constructors, nested types, and internal classes across assemblies.

The exact error

Assets/Scripts/Enemy.cs(14,16): error CS0122: 'PlayerHealth.TakeDamage(int)' is inaccessible due to its protection level
Assets/Scripts/Enemy.cs(15,26): error CS0122: 'PlayerHealth.health' is inaccessible due to its protection level

The name in quotes is the thing you are not allowed to use, written as Class.member. A method shows its parameter types, a constructor shows as 'SaveSystem.SaveSystem()', and a bare class name such as 'EnemyData' means the whole type is out of reach. The file and line are where you used it. The fix is usually made in the other file, where it is declared. This is a compile error, so it appears in the editor on recompile and in the build log, and nothing runs until it is fixed.

What “protection level” means, and the default you did not write

Every class and every member of a class has an access level that says which code may refer to it. You set it with a modifier in front of the declaration. When you write no modifier, C# picks the most restrictive level that makes sense:

Declaration with no modifierDefault accessConsequence in Unity
Field, method, property or constructor inside a class or structprivateint health; and void TakeDamage() cannot be used from another script.
Class, struct or enum declared inside another classprivateA helper class nested in Inventory cannot be named from outside Inventory.
Class, struct, interface or enum at the top level of a fileinternalclass EnemyData { } is visible only inside its own assembly.
Members of an interface, values of an enumpublicThese never cause CS0122 by themselves.

Unity’s default script template writes void Start() and void Update() with no modifier, so they are private. That works because Unity finds those message methods by name and calls them regardless of access. It does not carry over to your own methods: anything another script calls by name has to be visible to that script.

The access modifiers, and who can see what

ModifierSame classSubclass, same assemblyOther class, same assemblySubclass, other assemblyOther class, other assembly
publicYesYesYesYesYes
protected internalYesYesYesYesNo
internalYesYesYesNoNo
protectedYesYesNoYesNo
private protectedYesYesNoNoNo
privateYesNoNoNoNo

An assembly is one compiled unit of code. In a Unity project with no assembly definition files, almost all of your scripts compile into one assembly called Assembly-CSharp, which is why internal and public seem interchangeable at first. The difference shows up with Editor folders, Plugins folders, .asmdef files and packages, covered in Fix 4.

Two related errors use different wording for the same kind of problem:

What causes it, and how to tell which case is yours

  1. A field or method on another script has no modifier, or is marked private. The message names 'OtherClass.member'.
  2. The field is [SerializeField] private. The attribute makes it show in the Inspector. It does not make it visible to other code.
  3. A subclass uses a private member of its base class. Boss : Enemy reading health that Enemy declared without a modifier.
  4. A protected member is used from an unrelated class. Protected means subclasses only.
  5. A private constructor. new SaveSystem() where the class is a singleton that builds its own instance. The message ends in a constructor, 'SaveSystem.SaveSystem()'.
  6. A nested type. The message names 'Inventory.Slot', a class declared inside another class.
  7. An internal class used from another assembly. The message names a bare class. Typical when an Editor script, a test assembly or a script under an .asmdef uses a class declared without public.
  8. Unity or package internals. The quoted name starts with UnityEngine, UnityEditor or a package namespace. That API is not public.

To find your case, put the cursor on the name and use Go To Definition (F12 in Visual Studio and VS Code). Read the modifier on the declaration, or note that there is none. Then ask where your calling code sits relative to it: a different class (causes 1, 2, 4), a subclass (cause 3), or a different assembly (causes 7 and 8). If Go To Definition opens read-only decompiled or metadata code, the member belongs to Unity or a compiled plugin and you cannot change its modifier.

Fix 1: open up exactly what the other script needs

// PlayerHealth.cs, broken
public class PlayerHealth : MonoBehaviour
{
    [SerializeField] int health = 100;      // no modifier: private

    void TakeDamage(int amount)             // no modifier: private
    {
        health -= amount;
    }
}

// Enemy.cs
void OnCollisionEnter(Collision collision)
{
    PlayerHealth player = collision.gameObject.GetComponent<PlayerHealth>();
    player.TakeDamage(10);                  // CS0122: 'PlayerHealth.TakeDamage(int)'
    Debug.Log(player.health);               // CS0122: 'PlayerHealth.health'
}
// PlayerHealth.cs, fixed
public class PlayerHealth : MonoBehaviour
{
    [SerializeField] private int health = 100;

    public int Health => health;            // other scripts can read it

    public void TakeDamage(int amount)      // other scripts can call it
    {
        health = Mathf.Max(health - amount, 0);
    }
}

// Enemy.cs
player.TakeDamage(10);
Debug.Log(player.Health);

The quick fix is to write public in front of everything, and for a method that other scripts are meant to call that is the correct fix. For fields it is worth a second thought. A public field can be set to any value by any script, so a bug that leaves health at -40 could come from anywhere in the project. A private field with a public method keeps every change going through one place where you can clamp it, log it or put a breakpoint.

The three common shapes for exposing a value are:

With the second shape, gameManager.Score = 0; from another class gives CS0272. That is the property doing its job. Add a public ResetScore() method to the class that owns the value, or make the setter public if any script really should write it.

One Unity-specific point: the method list in a UI Button’s On Click () event in the Inspector only offers public methods. If your handler does not appear in the dropdown, it is private.

Fix 2: use protected for members that subclasses need

// Broken
public class Enemy : MonoBehaviour
{
    int health = 50;

    void Die()
    {
        Destroy(gameObject);
    }
}

public class Boss : Enemy
{
    void Update()
    {
        if (health <= 0) Die();             // CS0122 for 'Enemy.health' and 'Enemy.Die()'
    }
}

// Fixed
public class Enemy : MonoBehaviour
{
    protected int health = 50;

    protected virtual void Die()
    {
        Destroy(gameObject);
    }
}

Private means this class only, and a subclass is a different class. protected opens a member to subclasses and keeps it closed to everything else. Adding virtual lets Boss replace Die with protected override void Die() and still call base.Die().

Protected does not open the member to unrelated classes. enemy.Die() from a Bullet script still gives CS0122, and the fix there is a public method. There is also a narrower rule inside subclasses, reported as CS1540:

public class Boss : Enemy
{
    void Absorb(Enemy other)
    {
        health += other.health;
        // CS1540: Cannot access protected member 'Enemy.health' via a qualifier of type 'Enemy';
        // the qualifier must be of type 'Boss' (or derived from it)
    }
}

A subclass may use the protected members of its own instance and of other instances of its own type, and not those of an arbitrary Enemy. Expose a public read-only property for that.

Fix 3: singletons, private constructors and nested types

public class SaveSystem
{
    public static SaveSystem Instance { get; } = new SaveSystem();

    private SaveSystem() { }                // nobody else may construct one

    public void Save() { }
}

// Broken, in another script
SaveSystem save = new SaveSystem();         // CS0122: 'SaveSystem.SaveSystem()'

// Fixed
SaveSystem.Instance.Save();

A private constructor is deliberate: the class controls how many instances exist. Use the access point it provides, usually a static Instance property or a static factory method. The MonoBehaviour version of the pattern fails one step earlier, when the static field itself is private:

// Broken
public class GameManager : MonoBehaviour
{
    static GameManager instance;            // private

    void Awake() { instance = this; }
}
// elsewhere: GameManager.instance.AddScore(1);   // CS0122: 'GameManager.instance'

// Fixed
public class GameManager : MonoBehaviour
{
    public static GameManager Instance { get; private set; }

    void Awake() { Instance = this; }

    public void AddScore(int amount) { }
}

Nested types follow the member rule, so they are private unless you say otherwise:

public class Inventory : MonoBehaviour
{
    [System.Serializable]
    class Slot                              // private to Inventory
    {
        public string itemId;
        public int count;
    }
}

// Another script
Inventory.Slot slot;                        // CS0122: 'Inventory.Slot'

Write public class Slot. If you leave the nested class private and expose it through a public field, such as public List<Slot> slots;, the compiler reports a neighboring error on the field instead: CS0052, Inconsistent accessibility: field type 'System.Collections.Generic.List<Inventory.Slot>' is less accessible than field 'Inventory.slots'. The same fix applies. A public member cannot have a type that the outside world is not allowed to see.

Fix 4: internal classes and assembly boundaries

When the message names a bare class, the class is internal (often because it has no modifier) and the code using it is in a different assembly. Unity splits a project into assemblies along these lines:

Where the script livesAssembly it compiles into
A folder with an .asmdef file, or below oneThe assembly named in that .asmdef. This takes priority over the folder names below.
A packageThe package’s own assemblies
A folder named PluginsAssembly-CSharp-firstpass
A folder named EditorAssembly-CSharp-Editor (Assembly-CSharp-Editor-firstpass when it is inside Plugins)
Everything else under AssetsAssembly-CSharp

So a custom inspector in an Editor folder cannot see class EnemyData { } from your gameplay scripts, even though both are “your code”, and a test assembly cannot see the internal classes of the assembly it tests. There are two fixes.

Make the type public. Write public class EnemyData. For game code this is nearly always the right answer.

Grant one assembly access to another’s internals. This keeps the type internal and names the assembly that may see it. Put the attribute in any script of the assembly that owns the type. By convention the file is called AssemblyInfo.cs:

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Game.Tests")]
[assembly: InternalsVisibleTo("Assembly-CSharp-Editor")]

The string is the assembly’s name: the Name field of the .asmdef, not its file name, or one of the predefined names in the table. The other assembly must also reference this one, which is a separate setting and a separate error; see assembly definition reference errors.

Unity and package internals

If the inaccessible name belongs to Unity or a package (UnityEditor.LogEntries is a well-known example), it is internal to their assembly on purpose. It can change or vanish in any release, and you cannot add InternalsVisibleTo to an assembly you do not own. In order of preference: look for a public API that does the same job, check whether a newer version of the package exposes it, and only then consider reflection, accepting that it may break on an upgrade. For a package whose source you are prepared to maintain, copying it into your project’s Packages folder turns it into an embedded package that you can edit.

How to confirm the fix

Save both files, return to Unity and wait for the recompile. The entry leaves the Console, and compile errors cannot be removed with Clear, so a Console that stays empty is a real pass. If a new error appears on the same line, read its code. CS0272 or CS0200 means you can now see the property and may not assign it. CS0120 means you wrote the class name where an instance is needed. CS1061 means the member does not exist under that name at all, which is a different problem from not being allowed to use it.

If you changed a field from public to private with [SerializeField], its Inspector values are kept, because the field name did not change. If you renamed it while you were there, the values reset unless you add [FormerlySerializedAs("oldName")] from UnityEngine.Serialization.

Where Bugnet comes in

Not at this stage. CS0122 is settled by the compiler on your machine, no build is produced, and there is nothing for Bugnet to receive. Its relevance is to what the fix leaves behind. The line that hit the error usually fetches another script first, as in collision.gameObject.GetComponent<PlayerHealth>(), and that call returns null for every object that has no such component. Once the access problem is fixed and the game runs, the next failure on that line is a NullReferenceException. The Bugnet Unity SDK captures logged errors and exceptions through Application.logMessageReceived and files them with the stack trace, recent log lines, game version, platform and OS, and reports with the same title stack into one entry with an occurrence count.

What Bugnet cannot do is tell you which script wrote a bad value into a field you made public. A health value that ends up negative throws nothing, so it arrives, if at all, as a player’s description from the in-game report form. That is the practical argument for the property-and-method shape in Fix 1: one place to add a Debug.LogError when a value goes out of range, and that logged error is something the SDK does file. The SDK reference covers setup.

Frequently asked questions

What does error CS0122 mean in Unity?

The field, method, constructor or class you named exists, but its access modifier does not allow the script you are writing to use it. Most often it has no modifier, which makes a class member private. Make the member public, or expose it through a public property or method.

Why is my variable inaccessible when I never wrote private?

In C#, a field or method declared inside a class with no modifier is private by default. Writing int health; is the same as writing private int health; so only code inside that class can use it.

Can another script read a SerializeField private field?

No. The SerializeField attribute only tells Unity to save the field and show it in the Inspector, and the field stays private to its class. Add a public read-only property beside it, such as public int Health => health, and read that from the other script.

What is the difference between CS0122 and CS0272?

CS0122 means you cannot use the member at all. CS0272 means you can read a property but its set accessor is private, so you cannot assign to it from outside its class. Add a public method that makes the change, or make the setter public.

Why is a class inaccessible from my Editor script or test assembly?

A class declared without the public keyword is internal, which limits it to its own assembly. Scripts in Editor folders, in folders with an assembly definition and in packages compile into separate assemblies. Mark the class public, or add an InternalsVisibleTo attribute naming the assembly that needs it.

The member is there, and its declaration says who may use it. Go to the declaration, read the modifier (or the missing one), and open up only what the calling script needs: a public method or a read-only property beats a public field.