Unityでスクリプト内の変数をInspector画面で確認するには「public」もしくは「SerializeField」にする必要がありますが、debugモードを利用することで「private変数」も確認することが出来ます。
今回はUnityのInspector画面のDebugモードでprivate変数を確認するのと一意のIDを取得するのを確認していきます。
はじめに
下記の記事で作成した簡単なHPバーを利用していきます。
ボタンを押すと赤いバーが減ります。

スクリプトは下記を使用しています。
using UnityEngine; using UnityEngine.UI; public class HPBar : MonoBehaviour { [SerializeField] private Image _hpBarcurrent; [SerializeField] private float _maxHealth; private float currentHealth; void Awake() { currentHealth = _maxHealth; } public void UpdateHP(float damage) { currentHealth = Mathf.Clamp(currentHealth - damage, 0, _maxHealth); _hpBarcurrent.fillAmount = currentHealth / _maxHealth; } }
スクリプトを追加したオブジェクトには「SerializeField」の変数が表示されています。

ここでInspctorの三点から「Debug」を選択します。

private変数が表示されるので確認に使えます。また、InstanceIDと言うのも表示されます。

InstanceIDはランダムな一意となるIDで、下記の様な感じで取得することが出来ます。
void Awake() { currentHealth = _maxHealth; int i = GetInstanceID(); }
公式リファレンスは下記になります。
一意となるキーを簡単に取得したい場合に使えそうですね。