Featured Articles:

Wednesday, October 14, 2026

Your Unity Inspector Is Overriding Your Code: Here’s Why

Today we're doing something different from what you're used to.  Instead of laying the focus on coding, we're diving into Unity itself. Let's start from the beginning and talk about the panel you probably stare at more than your actual code: the Inspector. If you've ever typed a variable into a script, went back to Unity, and watched a neat little slot appear like magic, you've already met Unity Inspector fields. What you might not know is exactly why some variables show up and others quietly don't. And also, how to use the Inspector properly - the way it was intended.


Let's elaborate!


What Is The Purpose Of The Unity Inspector?

The Inspector is just an editor for the data stored on your components. When you add a script to a GameObject, Unity looks at the script's fields, picks the ones it knows how to save, and draws a matching control for each: a number box for an int, a checkbox for a bool, a color picker for a Color, a drag-and-drop slot for an object reference.

The values you type there get saved with the scene or prefab, not with the script. That one fact explains most of the surprises later in this article.

field in your script  →  Unity serializes it  →  Inspector draws a slot  →  value saved in the scene or prefab

So when we talk about Unity Inspector fields, we're really talking about "fields Unity is able to serialize and display." Let's look at which ones qualify.


Which Unity Inspector Fields Actually Show Up?

A field appears in the Inspector when two things are true: it's public or marked with [SerializeField], and its type is one Unity knows how to serialize. Here's what that looks like with a few common types:


public class Hero : MonoBehaviour
{
    [SerializeField] private int health = 100;          // number box
    [SerializeField] private float moveSpeed = 5f;      // number box
    [SerializeField] private bool isFlying;             // checkbox
    [SerializeField] private string heroName = "Hero";  // text box
    [SerializeField] private Color tint = Color.white;  // color picker
    [SerializeField] private Vector3 spawnOffset;       // x, y, z boxes
    [SerializeField] private Transform target;          // drag-and-drop slot
}
  

The types that work out of the box:

  • Basic types - int, float, bool, string
  • Common Unity types - Vector2, Vector3, Color, Quaternion, AnimationCurve
  • Enums - shown as a dropdown, which is lovely for things like weapon types
  • References to Unity objects - GameObject, Transform, any component, prefabs, AudioClip, Sprite, Material, ScriptableObjects
  • Arrays and Lists of any of the above
  • Your own classes and structs, as long as they're marked [System.Serializable]

And what Unity ignores: static, const, and readonly fields, properties, Dictionary, and any private field without the attribute.



How To Assign References In The Unity Inspector

Number boxes are easy. The real power is the reference slot, a field that points to another object. Here's a turret that needs to know what to aim at, what sound to play, and what to shoot:


public class Turret : MonoBehaviour
{
    [SerializeField] private Transform target;
    [SerializeField] private AudioSource shotSound;
    [SerializeField] private GameObject bulletPrefab;
}
  

To assign references in the Unity Inspector, you have three options:

  • Drag from the Hierarchy - for objects that live in your scene, like the player or an enemy
  • Drag from the Project window - for assets like prefabs, audio clips, and sprites
  • Click the little circle on the slot - opens a searchable picker, handy when your scene is a jungle

One rule worth knowing: a prefab asset sitting in your Project window can't hold a reference to an object that only exists in a scene. If a slot refuses your drop, this is a common reason.

Here's a tip I wish someone had given me earlier. Type your field as the component you actually need, not as a GameObject:


// Meh: you still have to dig the component out yourself
[SerializeField] private GameObject playerObject;

void Start()
{
    playerObject.GetComponent<Rigidbody>().mass = 2f;
}

// Better: drag the object in, Unity hands you the Rigidbody directly
[SerializeField] private Rigidbody playerBody;

void Start()
{
    playerBody.mass = 2f;
}
  

You still drag the same GameObject onto the slot. Unity finds the Rigidbody on it for you, and you skip the GetComponent call entirely.


Lists And Arrays In The Inspector

Need five spawn points? Please don't write five separate fields. Use a collection instead:


[SerializeField] private List<Transform> spawnPoints;
[SerializeField] private AudioClip[] footstepClips;
  

The Inspector shows each one as a foldout with a size field and a list of slots. You can click the plus button to add elements, or select several objects in the Hierarchy and drag them onto the list in one go. Lists and arrays look the same here, so the choice between them comes down to the same rules from the arrays vs lists article.


Inspector Values vs Code Values: The Gotcha That Gets Everyone

Ever changed a value in your code, hit Play, and watched absolutely nothing change? This is why.

The default you write in code is only the starting value for a brand new component. Once Unity has serialized the field, the Inspector's value wins.


// You wrote this first:
[SerializeField] private int health = 100;

// Later you change the default:
[SerializeField] private int health = 200;

// Existing components in your scene still say 100.
// Only newly added components start at 200.
  

To pick up the new default on an existing component, type the new value into the Inspector, or open the component's three-dot menu and choose Reset, which restores the defaults from your code. Just remember that Reset wipes every field on that component, not only the one you changed.


Unity Inspector Not Showing Variable? Run This Checklist

It's one of the most common questions new Unity devs search for, and the cause is almost always one of a handful of things. When you hit a Unity Inspector not showing variable situation, run down this list:

  • It's private with no [SerializeField]
  • It's a property, not a field. public int Health { get; set; } won't show up
  • It's static, const, or readonly
  • The type isn't supported, like Dictionary or an interface
  • It's your own class, but the class isn't marked [System.Serializable]
  • The script has a compile error, so Unity hasn't refreshed. Check the Console
  • The class name doesn't match the file name, so Unity won't treat it as a component

That fifth one deserves an example, since custom classes are where this gets people most:


[System.Serializable]
public class WeaponStats
{
    public int damage = 10;
    public float range = 5f;
}

public class Weapon : MonoBehaviour
{
    [SerializeField] private WeaponStats stats; // now shows as a foldout
}
  

Remove that [System.Serializable] line and the stats field vanishes from the Inspector. Add it back, and you get a tidy foldout holding both values.


Empty Slots: None And Missing

An unassigned reference slot reads None. If a slot says Missing, the object it pointed to was deleted. Either way, using that reference in code throws a NullReferenceException.

A cheap guard saves a lot of head scratching:


void Awake()
{
    if (shotSound == null)
    {
        Debug.LogError("Shot sound is not assigned in the Inspector.", this);
    }
}
  

Passing this as the second argument means you can click the error in the Console and Unity highlights the exact object that forgot its homework.


So Which Approach Should You Actually Use?

Here's the no-fluff version, the gut check you can run through in your head:

  • Tweakable number, text, color, or toggle → [SerializeField] private field
  • Need another object in the scene → type the field as the component and assign references in the Unity Inspector
  • Need an asset like a prefab or sound → same thing, drag it from the Project window
  • Need several of the same thing → a List or array
  • Group of related values → a [System.Serializable] class
  • Object only exists at runtime → you can't drag it in, so assign it from code
  • Variable missing from the Inspector → Unity Inspector not showing variable? Run the checklist above

The whole decision comes down to one question: "is this a value I want to tweak, or an object I need to point at?" Values become boxes, objects become slots, and both live in the Inspector instead of buried in your code.


Properties In The Inspector: The [field: SerializeField] Trick

Usually the Inspector ignores properties, which is a classic cause of a Unity Inspector not showing variable headache. But C# quietly creates a hidden backing field behind every auto-property, and the field: prefix lets you aim an attribute straight at it.


public class Hero : MonoBehaviour
{
    [field: SerializeField] public int Health { get; private set; } = 100;

    public void TakeDamage(int amount)
    {
        Health -= amount;
    }
}
  

That one line gives you a value that shows up in the Inspector as "Health," can be read by any script, and can only be changed from inside the class. It does the same job as the private field plus read-only property combo, in one line instead of two. The catch is that it only works on auto-properties, so no custom logic inside the getter or setter.


The Rename Gotcha: Don't Lose You're Saved Values

The hidden backing field is named after the property. Rename the property, and Unity sees a brand new field, so your saved Inspector values quietly reset to defaults. Ouch.

If you do rename one, tell Unity the old name:


using UnityEngine.Serialization;

[field: SerializeField, FormerlySerializedAs("<Health>k__BackingField")]
public int CurrentHealth { get; private set; } = 100;
  

That old name, <Health>k__BackingField, looks bizzare, but it's literally what the compiler calls it. If you'd rather skip that dance, the private field plus read-only property pattern avoids the problem, since the field keeps whatever name you gave it.


Wrapping Up

The Inspector isn't magic; it's just a window into whatever Unity can serialize from your scripts. Once you know which fields qualify, how to fill reference slots, and why Inspector values beat code defaults, most of the head-scratching moments disappear. Understanding how the Inspector exactly works is a giant step in the right direction, and it's something most beginner Unity developers overlook. Hopefully this article shone some light on this very intricate part of Unity game development.


 ~happy Coding!

No comments:

Post a Comment