Beginning C# with Unity (2023 Edition) - Operators

Vegetarian Zombie
22 Mar 202321:54

Summary

TLDRIn this educational video, Vegetarian Zombie guides viewers through the basics of C# operators in Unity, emphasizing the importance of celebrating small coding successes. The tutorial begins with an assignment operator, then explores incrementing and decrementing variables, and introduces equality checks. Sponsored by cadeco.com, the video also includes a practical demonstration of creating a button in Unity and responding to clicks using a script. The lesson covers arithmetic operations, the modulus operator, string concatenation, and the order of operations. It concludes with a challenge to create a tip calculator, encouraging viewers to apply their newfound knowledge.

Takeaways

  • 🎓 The video is an educational tutorial aimed at beginners in C# and Unity, focusing on the use of operators.
  • 👏 It encourages learners to celebrate small successes and be patient with themselves as they progress in coding.
  • 🔢 The video introduces the assignment operator, explaining how to use it to increment variables like hit points and experience points in a game.
  • 💻 Sponsored by cadeco.com, a platform for developers offering learning resources on various development topics, including game development in Unity and Unreal.
  • 🛠️ The tutorial demonstrates adding interactivity to a Unity project by creating a button and a script to respond to button clicks.
  • 📝 It discusses the importance of understanding operators for game development, such as increment, decrement, addition, subtraction, multiplication, division, and modulus.
  • 🧠 The video presents a brain teaser to illustrate the difference between postfix and prefix increment operators, highlighting a common source of confusion for new programmers.
  • 🔄 The concept of integer division is explained, showing how it truncates decimal values, and the modulus operator is introduced as a tool for remainder calculations.
  • 📑 The tutorial covers string concatenation and the need for manual spacing when combining strings with the addition operator.
  • 🧮 Emphasis is placed on the order of operations in arithmetic, advising viewers to write clear and simple code to avoid confusion.
  • 🔄 The cast operator is introduced for type conversion, with a cautionary note about data loss when converting from a larger to a smaller data type.
  • 💡 The video concludes with a challenge to create a tip calculator, providing a practical application of the concepts discussed.

Q & A

  • What is the main focus of the video?

    -The main focus of the video is to introduce the viewer to the world of operators in C# programming within the context of Unity game development.

  • Why is it important to celebrate small successes in coding?

    -Celebrating small successes in coding is important because coding is hard and it takes time to understand concepts. Recognizing and rewarding progress helps maintain motivation and patience.

  • What is the assignment operator in programming?

    -The assignment operator in programming, denoted by the equal sign, is used to assign the value of one variable to another, rather than to check for equality as in mathematics.

  • What is the purpose of the 'my operators' script created in the video?

    -The 'my operators' script is created to demonstrate how to respond to button presses in Unity and to show how to increment variables using different operators.

  • How does the video explain the difference between prefix and postfix operators?

    -The video explains that the prefix operator increments the value before the operation, while the postfix operator increments the value after the operation. It also cautions that the postfix operator can be confusing and recommends using the 'plus equals' operator for clarity.

  • Why does the video recommend avoiding the use of postfix operators when starting out?

    -The video recommends avoiding postfix operators for beginners because they can be easily misunderstood, leading to bugs and confusion, especially when the order of operations is not clear.

  • What is the modulus operator and how is it demonstrated in the video?

    -The modulus operator returns the remainder of a division operation. In the video, it is demonstrated by dividing 5 by 2, which results in a remainder of 1.

  • How does the video handle the addition of strings in C#?

    -The video shows that when adding strings in C#, it is necessary to manually add spaces between the strings to avoid them being concatenated without any separation.

  • What is the importance of the order of operations in coding as discussed in the video?

    -The video emphasizes the importance of the order of operations to ensure that calculations are performed correctly. It reminds viewers to follow the standard order (parentheses, exponents, multiplication/division, addition/subtraction) to avoid errors.

  • What is the cast operator and how is it used in the video?

    -The cast operator is used to convert a variable from one data type to another. In the video, it is used to convert an integer to a float and vice versa, demonstrating how data loss can occur when converting from a larger to a smaller data type.

  • What challenge is presented at the end of the video?

    -The challenge presented at the end of the video is to create a tip calculator that calculates the total amount including a tip based on the bill total and a tip percentage.

Outlines

00:00

📚 Introduction to C# Operators

The video, sponsored by cadeco.com, begins with a recap of the viewer's journey in learning C# and Unity, highlighting their progress from scripting basics to handling variables and types. The host, Vegetarian Zombie, encourages celebrating small victories in coding and introduces the concept of operators. The video then delves into the assignment operator, explaining its role in incrementing variables. It also touches on equality checking, which will be covered in future episodes. The sponsor, cadeco.com, is introduced as a platform for developers to learn various programming topics, including game development in Unity and Unreal. The tutorial proceeds with a practical demonstration of adding interactivity through a button in Unity, guiding viewers to create and attach a script that responds to button clicks.

05:05

🔗 Unity UI Interaction and Operator Basics

In this segment, the focus shifts to Unity's user interface, where the host guides viewers through attaching a script to a text element and setting up a button's 'OnClick' event to call a method from the script. The script is updated to include a method that increments a variable and displays its value, introducing the concept of shorthand operators for arithmetic operations. The video explains the difference between postfix and prefix increment operators, noting the potential for confusion with postfix operators. It advises beginners to use the 'plus equals' operator for clarity. The segment concludes with a brain teaser to reinforce understanding of operator precedence.

10:11

🧠 Understanding Prefix and Postfix Operators

This part of the video script discusses the nuances of prefix and postfix operators, emphasizing the importance of understanding their order of operation. It provides an example to illustrate how the postfix operator can lead to unexpected results if not used carefully. The video also covers the modulus operator, demonstrating how it returns the remainder of a division. The discussion extends to string concatenation and the importance of manually adding spaces. The segment ends with a reminder about the order of operations, advocating for simplicity and clarity in coding to avoid confusion and bugs.

15:13

🔢 Exploring Order of Operations and Casting

The script delves into the importance of order of operations in coding, using a social media puzzle as an example to explain the correct approach. It advises using parentheses for clarity and breaking down complex expressions. The video introduces the cast operator, showing how it can convert data types and the potential data loss when converting from a larger to a smaller type. The segment concludes with a challenge for viewers to create a tip calculator, providing a brief overview of the steps involved in setting up the calculator and connecting it to UI elements in Unity.

20:16

💻 Building a Tip Calculator in Unity

The final part of the script guides viewers through creating a tip calculator in Unity. It outlines the process of setting up a new script, adding variables for the bill total and tip percentage, and calculating the total amount including the tip. The video demonstrates how to connect the script to a button and text element in Unity, ensuring that the total amount is displayed when the button is clicked. It concludes with an encouragement for viewers to experiment with different approaches and to celebrate their progress, reminding them of the importance of writing clear and understandable code.

Mindmap

Keywords

💡Operators

Operators in programming are symbols that perform operations on variables and values. In the context of the video, operators are introduced as a fundamental concept in C# programming, essential for tasks such as increasing player's hit points or experience points in a game. The video covers various operators including assignment, arithmetic, and increment operators, highlighting their use in game development scenarios.

💡Assignment Operator

The assignment operator, denoted by the equal sign (=), is used to assign values to variables. The video explains that in programming, this operator is not about equality but about assigning the value on the right to the variable on the left, as in 'x = x + 1', which increments the value of 'x'.

💡Arithmetic Operators

Arithmetic operators include addition (+), subtraction (-), multiplication (*), and division (/). These operators are used to perform basic mathematical operations. The video demonstrates how to use these operators to manipulate game variables, such as increasing a player's strength or calculating the cost of an item.

💡Increment Operator

The increment operator is used to increase the value of a variable by one. The video distinguishes between the postfix and prefix increment operators, explaining their different behaviors in expressions. This is crucial for understanding how values are updated in game loops or event handlers.

💡Equality

Equality in programming often refers to the comparison of two values to check if they are the same. Although not deeply explored in the video, the concept is mentioned in passing when discussing the assignment operator, hinting at future episodes that will cover comparison operators in detail.

💡Variables

Variables are used to store data values that can change during the execution of a program. The video emphasizes the importance of variables in game development, such as storing player stats like hit points and experience points, and how operators are used to modify these values.

💡Methods

Methods are blocks of code that perform a specific task. In the video, a method is used to handle button clicks, demonstrating how to write a method that responds to user interactions in a Unity game. This introduces the concept of event-driven programming, where actions are triggered by user events.

💡Unity

Unity is a game development platform used to create video games. The video uses Unity as the environment for demonstrating C# programming concepts, showing how to integrate code with the Unity engine to create interactive game elements, such as buttons that trigger actions.

💡Casting

Casting is the process of converting a value from one data type to another. The video explains how to use casting to convert between different numeric types, such as from float to int, which is important when performing arithmetic operations that require specific data types.

💡Modulus Operator

The modulus operator returns the remainder of a division operation. In the video, it is used to demonstrate how to find the remainder after division, which is a concept that can be used in various game mechanics, such as determining the result of a dice roll or calculating scores.

💡Order of Operations

The order of operations, also known as PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction), dictates the sequence in which mathematical operations should be performed. The video emphasizes the importance of understanding this order to avoid errors in calculations, especially when writing complex expressions in code.

💡Codeco.com

Codeco.com is mentioned as a sponsor of the video and is described as a site for developers with resources for learning various development topics, including Unity and game development. This serves as an example of how educational content can be supported by sponsors that offer relevant resources to the audience.

Highlights

Introduction to operators in C# programming

Importance of celebrating small successes in learning to code

Explanation of the assignment operator for variables

Sponsorship message from cadeco.com for developer learning resources

Adding interactivity by creating a button in Unity

Creating a new script called 'my operators' in Unity

Responding to button presses with a method in C#

Updating the UI text in response to user interactions

Incrementing variables and shorthand operators in C#

Understanding the difference between postfix and prefix operators

Demonstration of integer division and the modulus operator

Concatenation of strings using the addition operator

Emphasizing the importance of order of operations in coding

Advice on writing clear and understandable code

Introduction to the cast operator for type conversion

Challenge: Creating a tip calculator program in Unity

Setting up a tip calculator with public fields for bill total and tip percentage

Calculating the total amount with tip and displaying it on the screen

Encouragement to celebrate completing the first program

Transcripts

play00:00

this video is sponsored by cadeco.com

play00:03

[Music]

play00:14

hey what's up everyone vegetarian zombie

play00:16

here from learn create play to walk you

play00:19

through the world of operators

play00:21

but before we talk code let's review

play00:24

Where We Are

play00:25

when you started this course you were

play00:27

probably new to C sharp and possibly

play00:30

unity in that time you created a small

play00:32

script to print messages on the screen

play00:34

you started getting comfortable with

play00:36

your code editor and you learned about

play00:38

variables and types that's a lot so give

play00:41

yourself a pat on the back coding is

play00:44

hard and it takes time this means you

play00:47

should really celebrate the small

play00:48

successes and be patient with yourself

play00:51

when it takes you a while to understand

play00:53

things

play00:54

now as is you know how to create

play00:56

variables you can create variables for

play00:59

hit points and experience points yet

play01:02

what happens when the player kills the

play01:04

boss how do you increase the player's XP

play01:07

or how do you increase their hip points

play01:09

when they take damage

play01:11

welcome to the world of operators

play01:13

there's a whole bunch of them but I'll

play01:15

just introduce you to some of the more

play01:16

common operators to get started we will

play01:19

start with the equal sign in math we use

play01:22

the equal sign to denote equality in

play01:25

programming it's known as the assignment

play01:27

operator let's take the following

play01:30

statement mathematically the statement

play01:32

doesn't make much sense a variable can

play01:35

never equal itself plus one

play01:38

instead what is happening is that you

play01:40

are incrementing X Plus 1 and then you

play01:44

are assigning the result of that

play01:46

increment back to X

play01:49

there's a way to check for equality but

play01:51

that will come in a later episode

play01:53

for now let's play around with operators

play01:56

but before I do here's a message from my

play01:58

sponsor codeco.com kadeco is a site for

play02:01

developers made by developers with

play02:04

hundreds of instructors from around the

play02:06

world you can learn about topics such as

play02:07

native iOS development native Android

play02:10

development and even multi-platform

play02:12

development with flutter

play02:14

kadeco also features hundreds of free

play02:16

articles including topics on game

play02:18

development covering both unity and

play02:20

unreal as a pro subscriber you can

play02:23

access a library of thousands of videos

play02:25

on a range of development topics the

play02:27

curated learning paths are designed to

play02:29

teach the basics of development in a

play02:32

friendly and supportive way Pro

play02:33

subscribers also have complete access to

play02:36

all the books at codeco such as the

play02:38

unity Apprentice that aims to teach you

play02:40

Unity by creating a series of different

play02:43

games get started on your programming

play02:45

Journey today by heading on over to

play02:48

codeco.com

play02:50

okay we are going to start this demo by

play02:53

adding a little interactivity to do this

play02:55

we're going to add a button to our

play02:57

project let's get started by opening

play02:59

your project in progress

play03:01

select the canvas game object in the

play03:04

hierarchy and click the plus sign select

play03:07

UI and then button text mesh Pro

play03:10

good work now arrange the button so that

play03:13

it is on the bottom of the screen

play03:22

we're going to create a new script Now

play03:24

call it my operators if you've been

play03:26

following this course you should already

play03:28

be familiar with creating Scripts

play03:40

open up the script and update it to the

play03:43

following

play04:28

you've seen this before it lets us write

play04:31

on the screen now let's write a little

play04:33

code to respond to button presses

play05:04

this is called a method we'll be

play05:07

covering methods in later episodes The

play05:09

key thing to note is all our code goes

play05:11

between the curly braces return back to

play05:14

the unity editor first select the text

play05:17

and remove our Mad Lib script from it

play05:22

simply find it in the inspector click

play05:25

the three dots and select the remove

play05:27

component option

play05:29

next drag our my operator script to it

play05:42

excellent now for the button

play05:45

select the button and scroll down in the

play05:48

inspector until you see the button

play05:50

component

play05:51

my button is named button one so I'm

play05:54

just going to rename it for now

play06:04

now look at the inspector at the very

play06:06

bottom is something marked on click

play06:11

click the plus sign beneath it

play06:14

this provides some options don't worry

play06:16

about what they do simply drag the text

play06:19

game object to the bottom option

play06:27

you'll see the drop down next to it

play06:29

brighten up click the drop down then

play06:32

find my operators

play06:37

from the fly out menu select the display

play06:40

message method

play06:43

we've just told the button to call that

play06:45

method when the user clicks on it that's

play06:47

all I'm going to say about this right

play06:49

now you are getting used to working with

play06:51

unity's user interface from a very high

play06:54

level you'll have to trust me on this we

play06:56

want to focus on c-sharp and not get

play06:59

lost in unity but we're doing lots of

play07:02

important practice think of Danielson

play07:04

from Karate Kid I'm thinking of the 80s

play07:07

version I never saw the Remake when he

play07:09

did all these monotonous chores when in

play07:12

fact he was actually building the muscle

play07:14

memory for his karate that's what we're

play07:17

doing here

play07:18

okay now Run the game and I use the word

play07:21

game in the loosest of terms

play07:26

and click the button

play07:29

great we got a message on the screen

play07:32

okay let's go back to our code

play07:35

in our new method add the following

play07:44

we've just created a new variable we

play07:47

don't need a public or private modifier

play07:49

inside a method you'll learn why in a

play07:52

later episode now let's increase it by

play07:55

10.

play08:00

update the text

play08:10

and Run the game

play08:14

we get one thousand we've incremented

play08:17

our variable you're going to do this a

play08:19

lot so there is shorthand code for it

play08:23

back in the code add the following

play08:32

this is the same as the previous

play08:34

statement it's just condensed we can

play08:38

also subtract

play08:42

multiply

play08:48

or divide

play08:54

there's nothing magical about the code

play08:56

it's just shorthand to save you time now

play09:00

if you want to increment just by one you

play09:02

can use the postfix and prefix operators

play09:33

here we've incremented the strength stat

play09:35

by one

play09:39

and here we get our strength increment

play09:41

that's the prefix the post-fix also

play09:45

increments the value

play09:59

here's a brain teaser what will be

play10:02

printed on the screen take a moment to

play10:04

think about it now let's run it

play10:10

look at that it's still 18.

play10:13

now let's print out the strength

play10:22

it's 19 now

play10:24

let's take a look at what's happening

play10:26

here our strength variable is set to 18.

play10:30

then the value is assigned to the stat

play10:32

variable

play10:34

after the assignment the value is

play10:36

increased by one by way of the

play10:38

post-fixed operator

play10:40

thus the post-fixed operator occurs

play10:43

after any operation

play10:45

let's take a look at this statement here

play10:47

if strength was 18 in this case the stat

play10:51

variable would then be 36.

play10:54

after that multiplication and assignment

play10:56

the strength variable would be

play10:58

incremented to 19.

play11:01

if you are confused welcome to the club

play11:03

while the prefix operator is pretty easy

play11:06

to grasp the post-fix operator can be

play11:09

easily misunderstood there have been so

play11:12

many bugs from misunderstanding their

play11:14

applications that they were removed

play11:16

entirely from the Swift programming

play11:18

language yet they are in wide use in C

play11:20

sharp so you need to know about them my

play11:23

recommendation is to just use the plus

play11:25

equals operator until avoid these

play11:27

operators when starting out personally I

play11:30

avoid them entirely

play11:32

now let's play around with division take

play11:34

this code

play11:42

okay guess the result and yes this is a

play11:45

trick question

play11:48

when we run the code we get 2 not 2.5

play11:52

this is because integers do not contain

play11:55

any decimal information it's just plain

play11:58

old numbers baby

play12:00

any floating Point information is

play12:02

dropped

play12:03

now we have the modulus operator let's

play12:06

change the division sign into the

play12:08

following

play12:10

When We Run the game we get one can you

play12:13

guess what this operator does

play12:15

it Returns the remainder of a division

play12:18

operation 2 goes into 5 twice and

play12:21

afterwards one is left over and that's

play12:24

the result that the modulus operator

play12:26

returns

play12:27

the modulus operator is commonly used to

play12:30

determine whether a number is even or

play12:32

odd if the result is zero you know you

play12:35

are working with an even number

play12:37

so we've played with addition with

play12:39

numbers but how about addition with

play12:41

strings let's try it out

play13:04

okay so what's going to print out on the

play13:07

screen I asked pretending this isn't a

play13:09

trick question but it totally is a trick

play13:12

question let's run it

play13:15

we get the name printed out but there is

play13:18

no space between it we need to manually

play13:20

add it update your code to the following

play13:33

now one important thing to keep in mind

play13:36

when working with operators is the order

play13:38

of operations chances are you haven't

play13:41

thought about it since school but it's

play13:43

critical to your success you'll often

play13:46

see images on social media declaring

play13:48

only Geniuses can solve this when in

play13:51

truth it's just an order of operations

play13:53

question take this one

play13:56

if you completely forgot about the order

play13:58

of operations you may start on the left

play14:00

hand side and work right

play14:07

so in this case you might answer two

play14:11

three minus 3 equals zero times six is

play14:14

zero then plus two but that's the wrong

play14:18

answer order of operation States

play14:20

parentheses exponent division

play14:23

multiplication addition and subtraction

play14:26

so in this case we start with six times

play14:29

three now there's no other

play14:31

multiplication in division since

play14:34

addition and subtraction are on the same

play14:36

priority level it doesn't matter which

play14:38

comes first so we can start left and go

play14:41

right three minus 18 equals negative

play14:44

fifteen plus two equals negative

play14:46

thirteen we can write the same thing in

play14:49

code

play15:06

now let's run the game

play15:10

and here's our result okay technically

play15:13

that works but people sometimes forget

play15:15

the order of operations and also it's

play15:18

confusing at a glance opt for Simplicity

play15:21

over complexity

play15:24

whenever you find yourself getting too

play15:26

clever with code remember that people

play15:29

don't play games for the code they play

play15:31

games to have fun now I might write a

play15:35

problem like this

play15:43

parentheses always goes first so use

play15:46

parentheses to your advantage also don't

play15:48

be afraid to break up complicated math

play15:50

into separate statements

play16:17

it might not look too Cody or hacker

play16:20

Elite but again we're opting for

play16:22

Simplicity by emphasizing Clarity your

play16:25

code is easier for other people to

play16:26

understand but more importantly it's

play16:29

easier for you to understand trust me

play16:31

and this happens to me all the time

play16:33

months later you'll be reviewing your

play16:36

own code and be pulling a Gandalf

play16:38

impression this is why it's important

play16:40

for yourself and others to write clear

play16:43

code

play16:45

lastly we have the cast operator this

play16:48

converts types say you want to convert a

play16:50

float to an INT you simply provide the

play16:53

type you want to use in parentheses this

play16:56

converts the integer 100 into a floating

play16:59

Point number now if you try to convert a

play17:02

larger type into a smaller type you're

play17:04

going to lose data since a smaller type

play17:06

can't hold all the information of a

play17:08

larger type

play17:09

the cast basically tells the compiler

play17:12

that you accept the data loss

play17:36

in this case we've converted a 32-bit

play17:39

number into an 8-bit number

play17:43

needless to say a lot of data was lost

play17:47

okay that concludes operators but the

play17:49

fun is just beginning for your challenge

play17:52

I want you to create a tip calculator

play17:54

create a public field that is a bill

play17:57

total create another field that is a tip

play18:00

percentage when the user presses the

play18:03

button the total amount should be

play18:05

displayed on the screen if you want to

play18:07

take this to the next level create a new

play18:09

script called tip calculator and make

play18:12

sure to attach it to the text and

play18:14

connect the button

play18:15

and now as a hint this task requires

play18:18

some casting pause the video now and try

play18:21

it out

play18:22

God

play18:24

[Music]

play18:27

you can't put out this fight

play18:36

okay we're back and ready to create a

play18:39

tip calculator believe it or not this is

play18:41

your first actual program granted it's a

play18:44

tiny one but it's a good first step okay

play18:47

first I get started by creating a script

play18:49

called tip calculator

play18:59

I open the script set it up and add a

play19:02

new method

play19:50

I add it to our text

play20:02

first I remove the my operator script

play20:07

then I select the button in the

play20:09

inspector I add the text back to the on

play20:11

click event

play20:16

from the drop down I select my object

play20:18

and choose the calculate tip method

play20:21

then I switch back

play20:25

next I create two variables

play20:46

okay here's where the magic happens

play20:48

first we calculate the tip amount

play20:50

variable by multiplying the amount

play20:52

variable times the percentage variable

play20:55

since the percentage variable is a float

play20:58

we need to convert the amount variable

play21:00

to a float as well as we can't multiply

play21:03

a float with an integer next we

play21:06

calculate the total amount we do this by

play21:08

converting our tip amount variable to an

play21:10

integer and adding the amount variable

play21:15

and that's it switch back to Unity in

play21:19

the tip calculator set the amount to

play21:20

100.

play21:26

now we get 120 as a total amount nice

play21:29

work if you did something different but

play21:31

it works no worries at all there's lots

play21:34

of ways to do the same thing just make

play21:36

sure to Pat yourself on the back and say

play21:38

good job

play21:39

in any case I hope you enjoyed this

play21:41

episode if you did make sure to click

play21:43

that like button and also if you want to

play21:46

be notified about more c-sharp videos

play21:48

make sure to subscribe and enable your

play21:51

notifications I'll see you in the next

play21:53

one

Rate This

5.0 / 5 (0 votes)

関連タグ
C# TutorialUnity CodingBeginner GuideCode SponsorCADECOGame DevelopmentVariable TypesOperatorsCodeco.comProgramming
英語で要約が必要ですか?