Final Lecture 2
Final Lecture 2
Lecture 2
Script
● A script is a chunk of code that attached to game objects to define their behaviors.
● 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.
6
Creating a New Script
● The Debug.Log() command prints a
message to the Console view in
Unity.
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.
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.
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.
● When declaring variables, you should state what kind of information they will
store by defining a data type.
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.
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.
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.
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.
25
User Interface (UI) – Button Object
● For changing the source image of
the button:
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.
28
User Interface (UI) – Input Field Object
29
THANK YOU