Public vs Private in Unity: Key Differences and When to Use Each
public means a variable or method can be accessed from other scripts and is visible in the Inspector, while private means it is only accessible within the same script and hidden in the Inspector by default. Use public to share or edit values easily, and private to keep data safe inside the script.Quick Comparison
Here is a quick side-by-side look at public and private in Unity scripts.
| Aspect | Public | Private |
|---|---|---|
| Access from other scripts | Allowed | Not allowed |
| Visible in Inspector | Yes | No (unless using [SerializeField]) |
| Default access modifier | No (must specify) | No (must specify) |
| Use case | Expose variables/methods | Hide variables/methods |
| Encapsulation | Less secure | More secure |
Key Differences
public variables and methods in Unity are accessible from any other script. This means you can easily share data or call functions across different parts of your game. Additionally, public variables show up in the Unity Inspector, allowing you to change their values without modifying code.
On the other hand, private variables and methods are only accessible inside the script where they are declared. They do not appear in the Inspector by default, which helps keep your data hidden and protected from accidental changes. This is useful for internal logic that should not be changed externally.
Unity also allows you to make private variables visible in the Inspector by adding the [SerializeField] attribute. This keeps the variable hidden from other scripts but editable in the Inspector, combining encapsulation with convenience.
Code Comparison
Here is an example showing a public variable and method in a Unity script.
using UnityEngine; public class Player : MonoBehaviour { public int health = 100; // Visible in Inspector and accessible from other scripts public void TakeDamage(int damage) { health -= damage; Debug.Log("Health is now: " + health); } }
Private Equivalent
This example shows the same logic but with private variables and methods. The variable is hidden from other scripts and the Inspector by default.
using UnityEngine; public class Player : MonoBehaviour { private int health = 100; // Not visible in Inspector or accessible from other scripts private void TakeDamage(int damage) { health -= damage; Debug.Log("Health is now: " + health); } private void Start() { TakeDamage(10); // Can call private method inside the same script } }
When to Use Which
Choose public when you want other scripts to access or modify a variable or call a method, or when you want to adjust values easily in the Inspector during development. This is common for settings or data that need to be shared.
Choose private when you want to protect data and logic inside a script, preventing other scripts from changing it accidentally. Use private to keep your code organized and safe, especially for internal calculations or temporary states.
Use [SerializeField] with private if you want to keep variables hidden from other scripts but still editable in the Inspector.