0
0
UnityComparisonBeginner · 4 min read

Public vs Private in Unity: Key Differences and When to Use Each

In Unity, 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.

AspectPublicPrivate
Access from other scriptsAllowedNot allowed
Visible in InspectorYesNo (unless using [SerializeField])
Default access modifierNo (must specify)No (must specify)
Use caseExpose variables/methodsHide variables/methods
EncapsulationLess secureMore 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.

csharp
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);
    }
}
Output
Health is now: 90 (when TakeDamage(10) is called)
↔️

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.

csharp
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
    }
}
Output
Health is now: 90 (logged when the game starts)
🎯

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.

Key Takeaways

Public members are accessible from other scripts and visible in the Inspector by default.
Private members are only accessible within their own script and hidden in the Inspector unless serialized.
Use public for shared data and private for encapsulation and safety.
Add [SerializeField] to private variables to edit them in the Inspector without exposing them to other scripts.
Choosing the right access level helps keep your Unity project organized and bug-free.