Documentation

Unity Engine

Download Unity

Unity Engine

Introduction to programming in Unity

Understand the role of scripting in your project and how it works at a high level.
Read time 4 minutesLast updated 6 days ago

Unity is customizable and extensible by design and almost everything is scriptable to some extent. Many items you can configure through the various Editor views have a corresponding public C# class representation that you can interact with in code.
You can use Editor APIs to customize and extend the Editor authoring tools to improve your development workflows. You can use Engine APIs to define the runtime functionality of your application, including graphics, physics, character behavior, and responses to user input.
The Scripting API reference provides the complete and authoritative reference for all public Unity APIs. The Manual provides additional context and guidance.

The Unity scripting environment

Unity supports scripting in the C# programming language. C# (pronounced C-sharp) is a managed, object-oriented programming language, which is part of the .NET platform and runs in the cross-platform .NET runtime. You can use other .NET languages with Unity if they can compile a compatible DLL. For more information, refer to Managed plug-ins.
The scripting environment refers to both:
  • Your own local environment or context in which you're writing code. This includes your code editor (IDE) and integrated source control solution and operating system.
  • The C# scripting environment Unity provides. A given version of Unity supports given versions of the .NET platform, which determines the .NET libraries you can use in your code.
For more information on the scripting environment and tools, refer to Environment setup.

How scripting in Unity works

C# scripts (files with a
.cs
file extension) are assets in your project, stored in the
Assets
folder and saved as part of the Asset Database. To create template scripts that derive from the common built-in Unity types, navigate to Assets > Create > Scripting.
You configure a default External Script Editor, which is the program Unity opens your script assets in for editing. For more information about the IDEs that Unity supports for development, refer to Integrated development environment (IDE) support.
You can create your own regular C# types and logic to use in your application, as long as the code you write is compatible with the active .NET profile. But your scripted types gain additional functionality in Unity when they inherit from a built-in Unity type.
If your custom types inherit from UnityEngine.Object, you can assign them to fields in the
Inspector

?

window. If your script inherits MonoBehaviour, you can attach it to a GameObject as a component to control the behavior of a GameObject in a scene.
For more information on fundamental Unity types you can inherit from, refer to Unity scripting types.
For more information on viewing scripts and editing script components in the Inspector window, refer to Scripts in the Inspector window.

Editor and runtime scripts

There are two distinct contexts in which your code might run:
  • In the Unity Editor at authoring time, usually as part of custom editing tools and windows that support your own development process.
  • In your application at runtime, as part of your user's experience.
Source files that contain Editor-only code are commonly referred to as Editor scripts. You can exclude Editor scripts from Player builds either by placing them in folders named
Editor
or by using assembly definition files to define Editor-only assemblies. For more information, refer to Editor folder.
To conditionally compile regions of code for Editor use only, use the
#if UNITY_EDITOR
preprocessor directive. For more information, refer to Conditional compilation.
The majority of Unity's core public APIs are in either the
UnityEditor
or
UnityEngine
namespaces. APIs in the
UnityEditor
namespace are only available in the Editor and attempting to use them in runtime code produces compilation errors.
While working in the Editor, you might have some code that you want to run in both Edit mode and Play mode. You might call some
UnityEditor
APIs while in Play mode for testing, visualization, or triggering asset operations. Likewise, you might want to call
UnityEngine
APIs in Edit mode by using
[ExecuteInEditMode]
or
[ExecuteAlways]
attributes on those sections of code.
Always exclude Editor scripts and code that uses
UnityEditor
APIs from your runtime Player builds.

Compilation and code reload

Compilation transforms the C# code you write into code that runs on a given target platform. Some aspects of compilation are under your control and others aren't. By organizing your scripts into assemblies you can reduce unnecessary recompilation and manage your dependencies effectively. With conditional compilation you can selectively include or exclude sections of your code from compilation.
Depending on your settings, Unity recompiles and reloads your code in various contexts. Reloading code is important for changes to take effect or to preserve state when transitioning between Edit mode and Play mode, but it also affects performance and iteration times. It's important to understand these costs and how you can configure Unity's code reload behavior to mitigate them.

Additional resources