# Access Modifiers

### <span style="color: #3598db;">What Are Access Modifiers?</span>

All types and type members in C# have an accessibility level and access modifiers are for controlling where they can be used from.

<table border="1" id="bkmrk-caller%27s-location-pu" style="border-collapse: collapse; width: 100%; height: 181.578px;"><tbody><tr style="height: 32.5938px;"><td style="width: 29.2954%; height: 32.5938px;">**Caller's location**

</td><td style="width: 8.65266%; height: 32.5938px;">**public**</td><td style="width: 16.9345%; height: 32.5938px;">**protected internal**</td><td style="width: 11.2485%; height: 32.5938px;">**protected**</td><td style="width: 9.2707%; height: 32.5938px;">**internal**</td><td style="width: 16.6877%; height: 32.5938px;">**private protected**</td><td style="width: 8.03415%; height: 32.5938px;">**private**</td></tr><tr style="height: 29.7969px;"><td style="width: 29.2954%; height: 29.7969px;">Within the class</td><td style="width: 8.65266%; height: 29.7969px;">✔️️</td><td style="width: 16.9345%; height: 29.7969px;">✔️️</td><td style="width: 11.2485%; height: 29.7969px;">✔️️</td><td style="width: 9.2707%; height: 29.7969px;">✔️️</td><td style="width: 16.6877%; height: 29.7969px;">✔️️</td><td style="width: 8.03415%; height: 29.7969px;">✔️️</td></tr><tr style="height: 29.7969px;"><td style="width: 29.2954%; height: 29.7969px;">Derived class (same assembly)</td><td style="width: 8.65266%; height: 29.7969px;">✔️️</td><td style="width: 16.9345%; height: 29.7969px;">✔️️</td><td style="width: 11.2485%; height: 29.7969px;">✔️️</td><td style="width: 9.2707%; height: 29.7969px;">✔️️</td><td style="width: 16.6877%; height: 29.7969px;">✔️️</td><td style="width: 8.03415%; height: 29.7969px;">❌</td></tr><tr style="height: 29.7969px;"><td style="width: 29.2954%; height: 29.7969px;">Non-derived class (same assembly)</td><td style="width: 8.65266%; height: 29.7969px;">✔️️</td><td style="width: 16.9345%; height: 29.7969px;">✔️️</td><td style="width: 11.2485%; height: 29.7969px;">❌</td><td style="width: 9.2707%; height: 29.7969px;">✔️️</td><td style="width: 16.6877%; height: 29.7969px;">❌</td><td style="width: 8.03415%; height: 29.7969px;">❌</td></tr><tr style="height: 29.7969px;"><td style="width: 29.2954%; height: 29.7969px;">Derived class (diff. assembly)</td><td style="width: 8.65266%; height: 29.7969px;">✔️️</td><td style="width: 16.9345%; height: 29.7969px;">✔️️</td><td style="width: 11.2485%; height: 29.7969px;">✔️️</td><td style="width: 9.2707%; height: 29.7969px;">❌</td><td style="width: 16.6877%; height: 29.7969px;">❌</td><td style="width: 8.03415%; height: 29.7969px;">❌</td></tr><tr style="height: 29.7969px;"><td style="width: 29.2954%; height: 29.7969px;">Non-derived class (diff. assembly)</td><td style="width: 8.65266%; height: 29.7969px;">✔️️</td><td style="width: 16.9345%; height: 29.7969px;">❌</td><td style="width: 11.2485%; height: 29.7969px;">❌</td><td style="width: 9.2707%; height: 29.7969px;">❌</td><td style="width: 16.6877%; height: 29.7969px;">❌</td><td style="width: 8.03415%; height: 29.7969px;">❌</td></tr></tbody></table>

<p class="callout info">[https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers)</p>

---

### <span style="color: #3598db;">What Access Modifiers Should I Use?</span>

UdonSharp 1.x supports all access modifiers for the source C# scripts to allow for better design of larger frameworks, so we can use them all just like we would with native C#.

<p class="callout success">Rule of thumb: Keep the access modifiers as strict as possible.</p>

---

### <span style="color: #3598db;">Common Mistakes</span>

Creators who may be new to C# development standards often expose everything in their classes by leaving fields and methods on ***public***, which in case of prefab distribution can lead to confusion.

<details id="bkmrk-example-1---fields-%7C"><summary>Example 1 - Fields \| Public vs Private + \[SerializeField\]</summary>

Simply setting the access modifier of a field to ***public*** is the easiest way of making it accessible in the Unity Editor, but it also exposes it in other scripts, which may be unwanted behavior.

We can change the access modifier of the field to ***private*** and add an attribute, ***\[SerializeField\]***. This will result in identical behavior on the inspector, but limits the scope of the field to that class only.

```C#
public bool publicField;

[SerializeField]
private bool privateSerializedField;
```

</details>