Unity - I Want to Display Variables in the Inspector! ~ About public and [SerializeField]~

### Magical and Godlike Syntax for Displaying Variables in the Inspector

What? You want to make variables displayable in the Inspector? I completely understand—variables are incredibly useful, aren’t they? Let me introduce you to the syntax that makes such variables displayable in the Inspector.

**Simply put, it’s `public`.**  
Let me explain with an example.

### The Convenience of Adding `public`

```c#:Test.cs
using System;
using System.Collections.Generic;
using UnityEngine;

public class TestScript : MonoBehaviour
{
    public int Int;
    public float Float;
    public double Double;
    public bool Bool;
    public char Char;
    public string String;
    public List<int> list;
    public int[] Array = new int[] {1, 2};
}
```

:::note warn
**Warning**  
When using List-type variables or array-type variables (as shown in the script above with `int[]`), always include:
```c#
using System;
using System.Collections.Generic;
```
Without this, an error will occur and it will not execute correctly.
:::

So, let’s say you write this process. You execute it—variables appear—and the result looks like this:

![Screenshot 2025-03-22 191300.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/4042593/dbd62cc5-4894-4531-94d7-699dbedcdb18.png)

Although `int[]` isn’t displayed for some reason, the execution result will look like this. (The List is added in the Inspector.)

The secret lies, as mentioned earlier, in `public`. This script not only makes the variables accessible to other classes but also displays them in the Inspector—a truly excellent feature.

### What’s Different Between `public` and `[SerializeField]`?

Among similar processes, there’s something called `[SerializeField]`. To use it, simply replace `public` with `[SerializeField]`.

Here’s when `[SerializeField]` can be used:
- When you want to display information in the Inspector
- When you don’t want other scripts to inadvertently modify the values

This process is especially helpful in team projects with two or more people. For example:

>A and B are working together on a project.  
>However, A and B are responsible for separate scripts.  
>A wants to display information in the Inspector and writes the following process:  
>```c#
>public float Object_Hearth;
>```  
>A, in fact, did not intend to make the variable `Object_Hearth` public, but B thought, “I can use `Object_Hearth`” and incorporated it into their script.  
>The project ultimately became problematic.  

To avoid such unfortunate results, `[SerializeField]` becomes very useful.[^1] Incidentally, A learned about this and corrected the process as follows:

```c#
[SerializeField] private float Object_Hearth;
//Using `private` clarifies that this object should not be accessed or used.
```

### Summary

This time, we discussed `public` and `[SerializeField]`.  

- `public` is effective when you want to display variables in the Inspector and make them accessible to other scripts.  
- `[SerializeField]` is effective when you want to display variables in the Inspector but **do not** want them to be accessible by other scripts.  

---

If you have any questions, feel free to ask! I’ll help as much as I can.
Translated with Bing AI

[^1]: Reference site: [【初心者Unity】[SerializeField]ってなに？ | TECH PROjin](https://tech.pjin.jp/blog/2021/12/23/unity-serializefield "【Beginner Unity】What is [SerializeField]? | TECH PROjin")