Unity
Lecture 2
Script
● A script is a chunk of code that attached to game objects to define their behaviors.
● Because interactive game programming is eventdriven, a typical script is
composed of a collection of functions, each of which is invoked in response to a
particular event. (e.g., A function may be invoked when this object collides with
another object.)
● Typically, each of these functions performs some simple action (e.g., moving the
game object, creating/destroying game objects, triggering events for other game
objects), and then returns control to the system.
2
Creating a New Script
● For writing some code, right click in the
Project panel and select Create → C# Script.
● This will create a new script in the Project
panel and highlight the filename.
● Name the Script and press ENTER to save
the script.
● Double click on the script to open it for
editing.
● Unity opens Microsoft Visual Studio, an IDE
for C#. If a different IDE is opened, go to
Edit → Preferences → External Tools →
External Script Editor to select an IDE.
3
Creating a New Script
● Once the script opens, you should have a
basic skeleton script. This will be the
default script every time you create a
new script.
● One important thing to check right away
is to make sure the class name of your
script matches the filename in Unity.
● The class HelloWorld inherits from the
MonoBehaviour which is the bass class
for all new Unity scripts. It contains a list
of all the functions and events that are
available to standard scripts attached to
GameObjects. 4
Creating a New Script
● Inheriting from MonoBehaviour
class provides a couple of methods
that are automatically included:
Start() and Update().
● Start() is called once when the
object becomes active (which is
generally as soon as the scene with
that object has loaded).
● Update() is called every frame.
● Note that, although the code is
written in Visual Studio, it isn’t run
there. The code is run when you
click Play within Unity. 5
Creating a New Script
Include namespaces for Unity and Mono classes
The syntax for inheritance
Put code here that runs once
Put code here that runs every frame
6
Creating a New Script
● The Debug.Log() command prints a
message to the Console view in
Unity.
● Meanwhile, that line goes in the
Start() method because, as was
explained earlier, that method is
called as soon as the object
becomes active.
● Once the log command is added,
save the script and go to Unity.
7
Using New Script
● Now, we need to create an object
in the scene to attach the script
to.
● Choose GameObject → Create
Empty, and a blank GameObject
will appear in the Hierarchy list.
● To assign the C# script to an
object in the scene, we can use
one of the following methods:
1) Drag the script from the
Project view and drop it on the
object in the Hierarchy view. 8
Using New Script
2) Select the object in the Hierarchy view so that the Inspector panel will display
its properties. Drag the script from the Project view and drop it anywhere
under the properties in the Inspector panel.
9
Using New Script
3) Select the object in the
Hierarchy view so that the
Inspector panel will display
its properties. Press the Add
Component button and pick
Scripts to select the specified
script in your project.
10
Using New Script
● To verify that the script is attached to the
object, select the GameObject and look at the
Inspector view.
● You should see two components listed: the
Transform component, which is the basic
position/rotation/scale component all objects
have and can’t be removed, and below that,
your script.
11
Printing Hello World! – Console Panel
● Click Play in Unity and switch to the Console view to see the message Hello
World!.
12
Printing Hello World! – Console Panel
● Note that, if you forget to type ; at the end of the line, an error message will
appear in the Console tab with a red error icon.
Script containing the error Description of this error
Location within this script
(line, character)
13
Variables
● Variables are simply containers for information.
● Variables may be named anything you like, provided that: 1) The name does not
conflict with an existing word in the Unity engine code. 2) It contains only
alphanumeric characters and underscores and does not begin with a number.
● Variables may contain text, numbers, and references to objects, assets, or
components.
● When declaring variables, you should state what kind of information they will
store by defining a data type.
● Here is an example of a variable declaration:
AccessModifier DataType VariableName = Value;
14
Variables
15
Public/Private Variables
● Variables that are declared as public, they
will automatically show up as parameters of
the script when it is viewed as a component
in the Inspector.
● For example, when attaching MyVariables
script to a GameObject and looking at the
Inspector view, we see its member variables
GameName, Score, WalkSpeed, and Jump
Speed.
● Unity introduces a space between a capital
letter occurs in the variable name.
● The value of each of these variables may then
be adjusted in the Inspector. 16
Public/Private Variables
● In contrast, variables that are declared using
the prefix private are hidden.
● Note that, any value adjusted in the
Inspector will override the original value
given to a variable within the script.
● It will not rewrite the value stated in the
script, but simply replaces it when the game
runs.
● You can also revert to the values declared in
the script by clicking on the Cog icon to the
right of the component and choosing Reset
from the drop-down menu that appears. 17
User Interface (UI) – Text Object
● It refers to the visual part of the interface such as
text, buttons, etc.
● TextMeshPro package in Unity is used to create
text displays.
● For creating a Text object in the scene, go to the
GameObject menu or RIGHT CLICK on Hierarchy
view and choose UI → Text-TextMeshPro.
● Creating a new UI element such as Text
automatically creates a Canvas in the scene
which is a rectangle area that all UI elements
should be inside. The UI element is created as a
child to this Canvas. 18
User Interface (UI) – Text Object
Type in text here
Change font style:
B → Bold
I → Italic Change font
U → Underline,
S → Strikethrough
Change the Change font size
ab → Lowercase
size of the Change font color
AB → Uppercase
text area
SC → Smallcaps
Apply gradient of
colors to text
Set Character –
Word – Line – Adjust the
Paragraph spacing horizontal and
vertical alignment
of the text
19
User Interface (UI) – Text Object
● Now, we need to create a new C# script and an
empty GameObject in the scene.
● Assign the C# script to the object.
● Finally, drag the Text object from Hierarchy
panel and drop it on TextMeshPro in the
Inspector settings of the GameObject.
20
User Interface (UI) – Text Object
21
User Interface (UI) – Text Object
22
User Interface (UI) – Button Object
● For creating a Button object in the scene, go
to the GameObject menu or RIGHT CLICK on
Hierarchy view and choose UI → Button-
TextMeshPro.
● Create a new C# script and an empty
GameObject in the scene to attach the script
to.
● Create OnButtonClick() method in the script.
23
User Interface (UI) – Button Object
Start
24
User Interface (UI) – Button Object
● Select the button and add an
OnClick event in the button
component.
● Drag the created GameObject to
the OnClick event.
● Select the OnButtonClick()
method we created.
25
User Interface (UI) – Button Object
● For changing the source image of
the button:
1) RIGHT CLICK on Project panel →
Import New Asset → choose the
image.
2) Select the asset in Project panel →
set the Texture Type in Inspector
view to Sprite (2D and UI).
3) Select the Button in the Hierarchy
view, drag the sprite image and
drop it on Source Image of image
component in the Inspector
settings of the Button. 26
User Interface (UI) – Input Field Object
First Number: 10
Second Number: 5
15
+ - * /
27
User Interface (UI) – Input Field Object
● For creating an Input Field object in the
scene, go to the GameObject menu or RIGHT
CLICK on Hierarchy view and choose UI →
Input Field -TextMeshPro.
● Create a new C# script and an empty
GameObject in the scene to attach the script
to.
28
User Interface (UI) – Input Field Object
29
THANK YOU