0% found this document useful (0 votes)
2 views

2.3 Control Structures - Loops and Conditional Statements in C# for Unity

This document provides an overview of control structures in C# for Unity, focusing on loops and conditional statements for programming tasks. It covers the setup and management of sprites, understanding prefabs and instantiation, and the destruction of game objects, along with basic movement scripting. The content is aimed at helping learners understand how to implement these concepts in game development using Unity.

Uploaded by

Quang Minh Cao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2.3 Control Structures - Loops and Conditional Statements in C# for Unity

This document provides an overview of control structures in C# for Unity, focusing on loops and conditional statements for programming tasks. It covers the setup and management of sprites, understanding prefabs and instantiation, and the destruction of game objects, along with basic movement scripting. The content is aimed at helping learners understand how to implement these concepts in game development using Unity.

Uploaded by

Quang Minh Cao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Control Structures - Loops and

Conditional Statements in C# for Unity


Learning Objectives

• Understand the role of control structures in programming.


• Learn how to use loops for repetitive tasks.
• Learn how to use selection and conditional statements for decision-making
• Learn how to set up your sprites and manage them with different components
• Understand Prefabs
• Understand Prefabs and Instantiation
• Understand Game Object Destruction
• Learn how to create basic movement

2
Loops in C#
for Loops

Image source: Unity.com


3
Loops in C#
for Loops

Image source: Unity.com


4
Loops in C#
foreach Loops

Image source: Unity.com


5
Loops in C#
foreach Loops

Image source: Unity.com


6
Loops in C#
while Loops

ITM
• Initialize the variables contained in the Boolean expression.
• Tests the Boolean expression to see if the loop body should execute or not.
• Modify at least one of the variables in the Boolean expression (give it a new value).

Image source: Unity.com


7
Loops in C#
while Loops

Image source: Unity.com


8
Loops in C#
while Loops

Image source: Unity.com


9
Conditional Statements

Image source: Unity.com


10
Conditional Statements
• A number of relational operators in C# that let us compare two things:
• ==, equal to
!=, not equal to
<, less than
<=, less than or equal to
>, greater than
>=, greater than or equal to

Image source: Unity.com


11
Conditional Statements

Image source: Unity.com


12
Conditional Statements

Image source: Unity.com


13
Switch Statements

Image source: Unity.com


14
Work with sprites
• Sprites are simple 2D objects that have graphical images (called textures) on them.
Unity uses sprites by default when the engine is in 2D mode. When viewed in 3D space,
sprites will appear to be paper-thin, because they have no Z-width.
• Sprites always face the camera at a perpendicular angle unless rotated in 3D space.

Image source: Unity.com


15
Work with sprites
Set up sprites:
1. Import your image
• Import sprites into project through one of the following methods:
• In your computer’s File Explorer (macOS: Finder), place your image directly into your Unity project’s
Assets folder. Unity detects this and displays it in the Project view.
• In Unity, go to Assets > Import New Asset. From the File Explorer (macOS: Finder) window that opens,
select your chosen image. Unity displays your chosen image in the Project view.
2. Set imported image as a sprite
• If you set your project to 2D, Unity automatically sets the image you import as a sprite. Unity will
also automatically create sprites based on the Automatic Slicing behavior in Sprite Editor
window. If you set your project to 3D instead, Unity imports your image as a texture.
• To change the asset’s Texture Type:
• Select the asset to access its Import Inspector
• Set the Texture Type to Sprite (2D and UI).
Image source: Unity.com
16
Work with sprites
• Whenever Unity makes a new sprite, it uses a texture. This texture is then applied on a fresh
GameObject, and a Sprite Renderer component is attached to it. This makes our GameObject
visible with our texture, as well as gives it properties related to how it looks on-screen.

Image source: Unity.com


17
Work with sprites
• To create a sprite in Unity, we must supply the engine with a texture.
• Let us create our texture first. Get a standard image file such as a PNG or JPG that you want to
use, save it, and then drag the image into the Assets region of Unity.

Image source: Unity.com


18
Work with sprites
• Next, drag the image from the Assets into the Scene Hierarchy. You will notice that as soon as
you let go of the mouse button, a new GameObject with your texture’s name shows up in the list.
You will also see the image now in the middle of the screen in the Scene View.

Image source: Unity.com


19
Work with sprites
Let us consider the following points while creating a sprite:
• By dragging from an external source into Unity, we are adding an Asset.
• This Asset is an image, so it becomes a texture.
• By dragging this texture into the scene hierarchy, we are creating a new GameObject with the
same name as our texture, with a Sprite Renderer attached.
• This sprite renderer uses that texture to draw the image in the game.

Image source: Unity.com


20
Understanding Prefabs and Instantiation
• Instantiating and destroying objects is considered very important during gameplay.
Instantiating simply means bringing into existence. Items appear or “spawn” in the
game, enemies die, GUI elements vanish and scenes are loaded all the time in-game.
Knowing how to properly get rid of unneeded objects and how to bring in those you do
then becomes even more essential.
• Prefabs are like blueprints of a GameObject. Prefabs are, in a way, a copy of a GameObject
that can be duplicated and put into a scene, even if it did not exist when the scene was being
made; in other words, prefabs can be used to dynamically generate GameObjects.

Image source: Unity.com


21
Understanding Prefabs and Instantiation
• To create a prefab, you simply have to drag the desired GameObject from your scene
hierarchy into the project Assets.

Image source: Unity.com


22
Understanding Prefabs and Instantiation
• To instantiate a GameObject, we call the Instantiate() method in our script. This
method, defined in MonoBehaviour, takes in a GameObject as a parameter, so it
knows which GameObject to create/duplicate. It also has various overrides for
changing the newly instantiated object’s transform, as well as parenting.
• Example: try instantiating a new hexagon whenever the Space key is pressed.
• Create a new script called Instantiator and open it up. In the Update method, type
in the code given below.

Image source: Unity.com


23
Understanding Prefabs and Instantiation
• Using the GetKeyDown method of the Input class to check if the player pressed a
specific button during the last frame
• Save the script, and let it compile. Once it is done, create a new, empty GameObject by
going to your object hierarchy right-click menu, and selecting Create Empty.

Image source: Unity.com


24
Understanding Prefabs and Instantiation
• Name this Object something recognizable such as Instatiator Object and attach our
newly created script to it. In the slot that shows up for the GameObject, drag in the
prefab we created.

Image source: Unity.com


25
Understanding Prefabs and Instantiation
• If we run the game now, pressing the Spacebar will create a new Hexagon object
identical to the one we used to create the prefab. You can see each hexagon being
created in the object hierarchy. The reason you cannot see them show up in the game is
because for the time being, they are all being created exactly one over the other.

Image source: Unity.com


26
Understanding Prefabs and Instantiation
• Name this Object something recognizable such as Instatiator Object and attach our
newly created script to it. In the slot that shows up for the GameObject, drag in the
prefab we created.

Image source: Unity.com


27
Understanding Prefabs and Instantiation
• If we run the game now, pressing the Spacebar will create a new Hexagon object
identical to the one we used to create the prefab. You can see each hexagon being
created in the object hierarchy. The reason you cannot see them show up in the game is
because for the time being, they are all being created exactly one over the other.

Image source: Unity.com


28
GameObject Destruction
• The destruction of GameObjects is as important as the instantiation.
• Use the Destroy() method to destroy a object:
Destroy (gameObject)
where gameObject is a reference to the object to be destroyed.
• It is important to understand that destroying a GameObject does not mean an object will
shatter or explode. Destroying an object will simply (and immediately) cease its
existence as far as the game (and its code) is concerned. The links to this object and its
references are now broken, and trying to access or use either of them will usually result
in errors and crashes.

Image source: Unity.com


29
GameObject Destruction
• Example: try to make 5 hexagons which will destroy themselves when an assigned key
is pressed.

Image source: Unity.com


30
GameObject Destruction
• Example:

Image source: Unity.com


31
Basic Movement Scripting
• Every GameObject has at least one component − Transform. What is special is that the
Transform of a gameObject also shows up as variables in the scripting side of Unity so
we can modify it via code. This is not restricted to the Transform either; all components
in Unity have properties, which are accessible through variables in scripting.

Image source: Unity.com


32
Basic Movement Scripting
• Example:
• Start with a movement script. Create a new script, and name it “Movement”.
• Open the script and you should see the same stuff you saw in the last lesson.
• Create a public float variable named speed. Making a variable public in Unity has a
great advantage.
• The variable shows up as a modifiable field inside the editor, so you don’t have to
manually adjust the values in code.

Image source: Unity.com


33
Basic Movement Scripting
• Example:
• Drag and drop the script from the Assets onto the GameObject. If you do it correctly,
this is what you should see in the GameObject’s properties.

• Since the speed value is adjustable and need not be changed in code all the time,
we can use update() method instead of start().

Image source: Unity.com


34
Basic Movement Scripting
• Example:
• Consider the objectives for the Update method:
• Check for the user input.
• If there is a user input, read the directions of input.
• Change the position values of the object’s transform based on its speed and direction.
To do so, we will add the following code

Image source: Unity.com


35
Basic Movement Scripting
• Example:
• Change the value of the speed in the GameObject’s properties to say 0.8. This is
important because a higher value will make the player move too fast.

Image source: Unity.com


36
Basic Movement Scripting
• Example:
• Click Play and see your first small game in action

Image source: Unity.com


37

You might also like