# スクリプトの検査

> View and edit the fields of your C# scripts in the Inspector window.

Project ウィンドウでスクリプトアセットを選択すると、Inspector には、そのアセットが属する[アセンブリ](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/script-compile-order-folders.md)の名前やコンテンツのプレビューなど、いくつかの基本情報が表示されます。

**注意**Inspector にはスクリプトのコンテンツが表示されますが、Inspector ウィンドウでコンテンツを編集することはできません。


**スクリプトの例が表示されたスクリプト Inspector。:**
![](/api/media?file=/engine/6000.0/media/images/ScriptInspector.png)

スクリプト Inspector には、**Open** と **Execution Order** の 2 つのボタンも表示されます。

**Open** は、Project ウィンドウでスクリプトをダブルクリックして、現在設定されている外部スクリプトエディターでスクリプトを開くのと同じ機能を実行します。Unity がスクリプトを開くために使う外部エディターは、[Preferences ウィンドウの外部ツールセクション](/engine/6000.0/manual/unity-editor/editor-features/preferences.md#external-tools)で設定できます。

**Execution Order** ボタンを押すと、[Project Settings ウィンドウの Script Execution Order セクション](/engine/6000.0/manual/unity-editor/editor-features/comp-manager-group/class-mono-manager.md)が開き、Unity がスクリプトを実行する順序を設定できます。

## Inspector ウィンドウのスクリプトコンポーネント##script-components-in-the-inspector-window

[MonoBehaviour](/engine/6000.0/manual/scripting/get-started/fundamental-unity-types/class-mono-behaviour.md) スクリプトはすべて[コンポーネント](/engine/6000.0/manual/working-in-unity/working-with-gameobjects/unity-components/components.md)として使用できます。つまり、

* スクリプトを[ゲームオブジェクト](/engine/6000.0/manual/working-in-unity/working-with-gameobjects/game-objects.md)にアタッチできます。
* スクリプトのプロパティと値は [Inspector ウィンドウ](/engine/6000.0/manual/unity-editor/using-the-editor/using-the-inspector.md)で編集できます。

以下のコード例では、`myName` という public フィールドを宣言しています。このスクリプトをシーンのゲームオブジェクトに追加すると、**Inspector** ウィンドウに **My Name** というラベルのフィールドが表示されます。スクリプトで宣言された `none` のデフォルト値が **Inspector** ウィンドウのデフォルト値になります。この値は、フィールドに入力して変更することができます。

```
using UnityEngine;
using System.Collections;

public class MainPlayer : MonoBehaviour 
{
	public string myName = "none";
	
	// Use this for initialization
	void Start () 
	{
		Debug.Log("I am alive and my name is " + myName);
	}
}
```

スクリプトコンポーネントをアタッチする各ゲームオブジェクトは、フィールドに一意の値を持つことができます。


**Inspector ウィンドウで編集可能な public 文字列フィールド。:**
![](/api/media?file=/engine/6000.0/media/images/EditingVarInspector.png)

フィールド名は、[フィールド名からラベルへの変換](/engine/6000.0/manual/scripting/get-started/inspecting-scripts.md#field-name-label-conversion)に記載されているルールに従って、**Inspector** ウィンドウのラベルに変換されます。ただし、これらの変更は単に表示上のものです。コードでは、常にフィールド名を使用する必要があります。

**Inspector** ウィンドウで、**My Name** の値を編集して Play を押すと、コンソールメッセージに入力したテキストが含まれるようになります。


**Frame:**
![](/api/media?file=/engine/6000.0/media/images/DebugLogMessage.png)

## Public フィールドと Private フィールド##public-and-private-fields

すべての `public` フィールドは、デフォルトで **Inspector** ウィンドウで編集可能です。パブリック変数が **Inspector** ウィンドウに表示されないようにするには、[HideInInspector](/engine/6000.0/script-reference/unity-engine/hide-in-inspector.md) 属性を追加します。**Inspector** ウィンドウで `private` フィールドを編集可能にするには、[SerializeField](/engine/6000.0/script-reference/unity-engine/serialize-field.md) 属性を追加します。

**ノート**: [再生モード](/engine/6000.0/manual/scripting/compilation-and-code-reload/code-reloading-editor/configurable-enter-play-mode.md)で実行中に、エディターのスクリプトのフィールドの値を変更できます。これは停止、再起動をせずに変更の結果をすぐ確認できるので、非常に便利です。ただし、再生モードが終了すると、フィールドの値は再生モードに入る前の値にリセットされます。

## オブジェクトリファレンスフィールド##object-reference-fields

`bool`、`string`、`int` などのシンプルな[ビルトイン C# 型](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types#built-in-value-types)だけでなく、[`UnityEngine.Object`](/engine/6000.0/manual/scripting/get-started/fundamental-unity-types/class-object.md) から継承した型のフィールドを **Inspector** ウィンドウで編集可能にすることもできます。これには、すべてのビルトインコンポーネントタイプ ([Transform](/engine/6000.0/manual/working-in-unity/working-with-gameobjects/gameobject-fundamentals/class-transform.md)、[AudioSource](/engine/6000.0/manual/audio/reference/class-audio-source.md)、[Camera](/engine/6000.0/manual/working-in-unity/cameras/birp/class-camera.md)、[Light](/engine/6000.0/manual/lighting-overview/lighting-birp/class-light.md) など)、独自の MonoBehaviour スクリプトタイプ、および多くのアセットタイプが含まれます。

これにより、独自のスクリプトコンポーネントで Unity エディターのドラッグアンドドロップシステムを使用できます。例えば、スクリプトに public `Transform` フィールドを作成し、それをあるゲームオブジェクトに追加すると、別のゲームオブジェクトを **Inspector** ウィンドウのそのフィールドにドラッグして、そのゲームオブジェクトの Transform コンポーネントへのリファレンスを設定できます。このリファレンスには、スクリプトでランタイムにアクセスできます。

例えば、この `Follow` スクリプトは、あるゲームオブジェクトを別のゲームオブジェクトに追従させます。

```
using UnityEngine;

public class Follow : MonoBehaviour
{
    public Transform objectToFollow;
    public float followSpeed = 1;

    void Update()
    {
		// calculate the distance between this object and the target object
		// and move a small portion of that distance each frame:

        var delta = objectToFollow.position - transform.position;
        transform.position += delta * Time.deltaTime * followSpeed;
    }
}
```

スクリプトには `Transform` 型の public フィールドがあります。これは、割り当て可能なフィールドとしてエディターに表示されます。別のゲームオブジェクトを Hierarchy ウィンドウからこのフィールドにドラッグアンドドロップすると、そのゲームオブジェクトにアタッチされている Transform コンポーネントへのリファレンスが、エディターによって割り当てられます。

以下のスクリーンショットでは、スクリプトはスフィアゲームオブジェクトに配置され、キューブは階層から **Object To Follow** フィールドにドラッグアンドドロップされています。


**ゲームオブジェクトが割り当てられた public Transform フィールド。ここでは、スクリプトは (現在選択されている) スフィア上に、キューブは階層からスフィアの Object To Follow フィールド :**
![](/api/media?file=/engine/6000.0/media/images/UnityObjectInspectorField.png) にドラッグアンドドロップされます。

## デフォルトのオブジェクト参照##default-object-references

MonoBehaviour スクリプトで[エディターに割り当て](/engine/6000.0/manual/scripting/get-started/inspecting-scripts.md#object-reference-fields)可能な public [オブジェクト](/engine/6000.0/script-reference/unity-engine/object.md)フィールドを定義する場合は、これらのフィールドのデフォルト参照を設定できます。デフォルトのリファレンスフィールドは、Project ウィンドウでスクリプトアセットを選択すると、Inspector に表示されます。


**3 つの AudioClip フィールドを持つ MonoBehaviour スクリプト。これらのフィールドのデフォルト参照は未設定のまま表示されます。:**
![](/api/media?file=/engine/6000.0/media/images/MonoBehaviourDefaultReferences.png)

上の例では、デフォルトの参照が割り当てられていない、3 つの public オーディオクリップフィールドがあります。各 AudioClip のデフォルトのリファレンスフィールドには、オーディオクリップを割り当てることができます。

デフォルトの参照を割り当てると、MonoBehaviour をゲームオブジェクトにコンポーネントとして追加したとき、またはゲームオブジェクト上の MonoBehaviour の既存のインスタンスをデフォルト値にリセットしたときに適用されます。

**注意**ゲームオブジェクトの MonoBehaviour インスタンスの参照とデフォルト参照の間には、継続的なリンクはありません。つまり、デフォルトの参照を変更しても、既存のゲームオブジェクトの参照は自動的に更新されません。

`UnityEngine.Object` から継承されていないその他のタイプの [Inspector 編集可能フィールド](/engine/6000.0/manual/scripting/get-started/inspecting-scripts.md) (public の string や int フィールドなど) には、Inspector のデフォルトフィールドはありません。代わりに、スクリプト自体からデフォルト値が使用されます。

## フィールド名からラベルへの変換##field-name-to-label-conversion

Unity は、一連のルールに従って、**Inspector** ウィンドウで C# フィールド名をラベルに変換します。例えば、上記の例の変数名は `myName` から **My Name** に、`objectToFollow` から **Object To Follow** に変換されています。変換のルールは以下のとおりです。

* 先頭の文字を大文字にします
* 小文字と大文字の間にスペースを加えます
* 頭文字と次の単語の先頭にある大文字の間にスペースを加えます
* `m_` プレフィックスを削除します
* `k` プレフィックスを削除します
* `_` プレフィックスを削除します

`iPad` や `x64` など、これらのルールが適用されない特殊なケースもあります。

## 追加リソース##additional-resources

* [コンポーネントの紹介](/engine/6000.0/manual/working-in-unity/working-with-gameobjects/unity-components/components.md)
* [Inspector ウィンドウ](/engine/6000.0/manual/unity-editor/using-the-editor/using-the-inspector.md)
