Working with Scripts

Tutorial

·

Beginner

·

+10XP

·

5 mins

·

(7572)

Unity Technologies

Working with Scripts

Scripts are behavior components that can be applied to GameObjects and modified in the Unity Inspector. A script is made up of C# code that is executed during the "play" state. Scripts can also be used to create tools in Unity to modify the development process. In this tutorial, you will learn about how to use scripts in Unity.

Languages available:

1. Working with Scripts

Video Overview


This tutorial has been verified using Unity 2019 LTS


Scripts are behavior components that can be applied to GameObjects and modified in the Unity Inspector. A script is made up of C# code that is executed during gameplay. Scripts can also be used to create tools in Unity to modify the development process. Scripts in Unity are developed using an IDE (integrated development environment) like Visual Studio.


2. Creating and Assigning Scripts

1. In the Project Window, right-click and choose Create > C# Script


2. Rename the script to ExampleScript


When renaming scripts, it is crucial that the name of the script matches the name of the class in the code. Unity will produce a warning and the script will not compile.


3. Drag and drop the script onto the necessary GameObject


3. Adding Code

1. Double-click on the new C# script


2. The script will open in the default scripting editor, either Visual Studio or MonoDevelop


3. The script will be populated with the default code seen below:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

4. Add the code in green to the existing script


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

5. Press File > Save (Ctrl+S or Cmd+S)


6. Add the script to a GameObject in the Scene


7. Press play, and in the console, note that the line “Hello World!” appears


4. Conclusion

Scripting is an essential part to creating gameplay. It can be a difficult skill to master, but the Unity Learn page is an incredible resource to increase your knowledge.


Complete this tutorial