C# SCRIPTS
Within Unity
Script Source Code
• This is the “source code” of a
script.
• A script is a small program, or
part of a program, that is
executed by a host program.
• Scripts are often interpreted, but
not always (e.g., Unity compiles
them).
• Coding, programming, and
scripting are all the same.
Using Declarations
The “using” declaration says what library parts
are used in the current file.
❑ Extra functionality is provided by libraries.
❑ Libraries are organized using “namespaces”.
❑ For example: two things called “List” might exist
in different libraries.
❑ Namespaces disambiguate: “MyLibrary.List”
and “System.Collections.List”.
❑ Delete them and see what happens.
Don’t Panic!
• This is a very common type of error, often because of misspelling, or a missing
“using directive”.
• Try double-clicking it.
• Now fix the mistake.
Class
• A class describes a particular
type of object.
• Objects contain data and provide
operations for accessing or
transforming that data.
• This is part of object-oriented
programming (OOP) and will be
covered in depth on later classes.
• In Unity, all scripts are classes.
Class Name
• The name of the script class must
match the file name.
• Try changing the name.
• You should see this in the inspector:
Inheritance
In Unity all scripts are classes that
inherit from a Unity class called
“MonoBehavior”.
This means that the things that a
MonoBehavior can do, and the
values that it contains, are
accessible to the new class.
This is called inheritance and is a
feature of object-oriented
programming (OOP).
Comments
• Comments are for humans and have
no impact on programs.
• The compiler ignores everything
from the “//” until the end of the
line.
• C# supports multi-line block
comments using “/*” and “*/”
• The C# compiler can generated help
documents if you use XMLDoc
format.
Functions
• The start function is called by
Unity once when the game
starts.
• The update function is called
by Unity every frame of the
game.
• They are not called when the
game object is deactivated.
Functions in Code versus
Mathematics
In Mathematics, a function from a set X to a set Y assigns
to each element of X exactly one element of Y.
In computer languages what are called “functions” are
actually “subroutines”.
Unlike mathematical functions:
▪ They might not return a value (“void”).
▪ They might have zero, one, or more inputs.
▪ They might have side-effects
▪ Calling them with the same input might yield different
results.
Type in your First Script
Attribute
Meta-information for the
compiler or host environment.
Available through Reflection.
Tells the script to run in editor
even when the game is not
playing.
Fields
• A field is named data
associated with an object. Also
known as a “member variable”.
• When declared as “public”
exposed in the editor as a
property.
Variable Declaration
• A variable is a name
associated with a value.
• The value associated with
the name can change.
• However, the type of data
(number, text, vector)
associated can’t be
changed.
• You can use the variable
name instead of
expression.
Variables are your friend
This is equivalent to the other script – which do you prefer?
Expressions
A sequence of symbols (operators, numbers, variables) that represent computations.
They are transformed into values (evaluated) when the program is executed.
Some examples include:
• Numbers: 42, 3.15
• Booleans: true, false
• Variables: x, MyVector, this_is_a_variable
• An operation with operands: x + 1, y >= 12, -z
• Parenthesized expression: 3 * (y – 2)
• A function call: Math.Sqrt(16)
• A member variable: this.NumRows
Expression Use Cases
Common uses of expressions are:
• operation input (operand)
• function input (argument)
• assigned to a variable
• return value of a function
• conditions for loop or branch statements
Practice: Find the Expressions
Statements
Statements are sequences of
instructions. They might declare
something, execute a subroutine,
or affect control flow.
For Loop Statement
• Calls the next statement
(the loop body) multiple
times.
• Executes an initialization
statement before starting.
• Only executes while the
invariant is true.
• After each loop iteration,
calls an iteration
statement.
Condition
• An expression (operation /
function call / constant)
with a value of type
Boolean (true or false).
• In the context of a for
loop is called the
invariant.
• Loop is executed while
condition is true.
Increment Statement
• Adds one to a variable.
• Returns the value of a
variable after adding one
to the variable.
• The same as “x = x + 1”.
• In the context of a for
loop, it is called after each
loop iteration.
Block Statements
• Also called a compound
statement.
• Allows any number of
statements (0 or more) to
be treated like one
statement.
Why are Loops Important?
The computer doesn’t care … you
could write out a statement 25 times.
What could possibly go wrong with this
approach?
An important thing to ask yourself
frequently when programming.
Function Calls
• An expression or
statement that executes a
function.
• If it returns a value, can be
used as an expression.
• Accepts types and/or
expressions as inputs.
Functions
• Functions are the fundamental
building blocks of a computer
program.
• Also known as procedures,
subroutines, or methods.
• Let’s write a function that draws
the mesh at a specified location
• Consider other ways we could have
written it