Quick Start


Using the Immediate Window

The Immediate Window for Unity replicates the interactive REPL (Read-Evaluate-Print Loop) interface familiar to most programmers and allows you to execute script expressions directly from the Unity editor, at both design time and while running your game or application in the editor. This can be quite useful during debugging because it allows you to print variables and entire object hierarchies, set property and field values, and directly execute methods on your GameObject and its components. Immediate Window uses a subset of the Javascript language when evaluating your expressions.

Opening the Immediate Window


immediate-window-menu

The Immediate Window can be launched by selecting the Immediate Window option in the Window menu. It can also be launched from the Tools menu, where it will be located under the StagPoint submenu. Selecting these menu options will open a floating Editor Window which can be docked wherever you feel it will be most convenient.

Executing Commands


To execute a command, type a Javascript expression into the text box highlighted in the snapshot above and either press the ENTER key or the Execute button. When an expression is executed, it will be printed in the output pane along with any results that are returned from the expression.

The output pane is scrollable, and will retain the last thousand lines of output. Use the clear() command to clear the output area.

The Immediate Window will remember the last ten expressions that were executed. When the input focus is in the command field, use the up and down arrow keys to scroll through the command history. Pressing the ENTER key will immediately execute the expression currently displayed.

Press the ESCAPE key to clear the input field.

 AutoComplete


immediate-window-autocomplete

The Immediate Window will provide an AutoComplete popup whenever it detects an expression that it can provide AutoComplete information for. Common examples include referencing an object’s member fields or properties, calling a method on an object, or using the new operator.

AutoComplete will filter the list of available options as you type. In the snapshot above, you can see that the user is attempting to instantiate a new object, and the Immediate Window is filtering all of the available types to only those with the letters “Vec” in the type name.

You may use the up and down arrow keys or the mouse to select the desired option. To accept the selected option, press the ENTER, SPACE or TAB key or click the option with the mouse. Pressing any of the following keys will also cause AutoComplete to accept the selected option: {}[]().,:;+-*/%&|^!~=<>?@#'”

You may hide the AutoComplete list at any time by pressing the ESCAPE key, and you can display the AutoComplete list by pressing the CTRL-J key combination.

 Script Expression Syntax


Immediate Window uses a subset of the Javascript language that should be familiar and easy to use for most Unity developers who use either C# or UnityScript, which are syntactically similar languages. You are limited to executing script expressions, which essentially means that you are limited to any code expression that returns a value. This includes assignments, arithmetic and comparison operations, function calls, variable declarations, etc. You cannot use statements such as loops, function declaration, if..else, or other code that is used for control flow rather than returning a value.

Note that Immediate Window uses a limited subset of Javascript, so not all operators will be available (specifically the Exact Equality/Inequality, Zero Fill Shift Right, typeof, in, instanceof, and void operators). Also note that type conversion rules follow the C# standard rather than the Javascript standard.

As an example, the following expressions are all valid:

Mathf.PI * 180
transform.position
transform.position = new Vector3( 10, 20, 30 )
$Agent.CanSeeEnemy( 60f )
var startTime = Time.realtimeSinceStartup

Accessing GameObjects


immediate-window-selected-gameobject

Whenever any GameObject is selected in the Hierarchy Tab, that object’s public fields, properties, and methods become accessible directly to your script expressions as can be seen in the snapshot above. This means that you can directly access any public member on the GameObject without having to use a qualifier. You may also use the this keyword to refer to the selected GameObject. When no GameObject is currently selected, you will only have access to global variables, types, and methods.

You may use the built-in find( string objectname ) function to find any GameObject currently in the scene.

Accessing Components


Whenever any GameObject is selected in the Hierarchy Tab, its components can be easily accessed as predefined variables whose identifiers consist of the name of the component type prefixed by the $ character. In the example above, you can see that the $TrunkThumper variable corresponds to the “Trunk Thumper (Script)” component in the Inspector tab to the right.

You can also use GetComponent( string componentType ) to obtain a reference to any component, which might be necessary for components that are added dynamically at runtime.

Note that the scripting language used in the Immediate Window does not support generics, so you must use the overload of GetComponent() that takes a string parameter rather than the GetComponent<T>() version.

Immediate Window creates a separate script environment for each GameObject, which means that variables defined while one Gameobject is selected will not be available when another GameObject is subsequently selected.

 Accessing Static Members


immediate-window-application-access

Immediate Window scripts have direct access to the static members of all built-in types, such as the Application type in the snapshot pictured above. This includes all of the public types defined in your own classes as well as the UnityEditor and UnityEngine assemblies. This makes it possible to use expressions like the following:

transform.scale = Vector3.one
var angle = 45 * Mathf.Deg2Rad
var distance = Vector3.Distance( transform.position, other.transform.position )
InventoryManager.HasItem( ItemType.Pistol, $agent.inventoryKey )
EditorPrefs.SetBool( "show-welcome-screen", false )